C++ C++;:对[函数]的未定义引用
给定一个C++ C++;:对[函数]的未定义引用,c++,C++,给定一个main.cpp文件: #include <iostream> #include <fstream> #include <string> #include <vector> #include "inoutfich.h" using namespace std; int main() { /* chargement du texte en mémoire dans une string */ char n_fichin[
main.cpp
文件:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "inoutfich.h"
using namespace std;
int main()
{
/* chargement du texte en mémoire dans une string */
char n_fichin[80];
cout << "Nom du fichier à copier : " ;
cin >> n_fichin;
vector<string> texte;
ChargeTexte(n_fichin, texte);
// cherche ligne la plus longue
unsigned iPLongue = LigneLaPlusLongue(texte);
cout << "La ligne la plus longue du texte est la ligne " << iPLongue;
cout << "**** " << texte[iPLongue] << " ****";
return 0;
} // main
当我编译
`$g++ main.cpp -o out.o`
我得到以下终端错误:
/tmp/ccunCT7N.o: In function `main':
main.cpp:(.text+0x5a): undefined reference to `ChargeTexte(char*, std::vector<std::string, std::allocator<std::string> >&)'
main.cpp:(.text+0x66): undefined reference to `LigneLaPlusLongue(std::vector<std::string, std::allocator<std::string> > const&)'
/tmp/ccunCT7N.o:在函数'main'中:
main.cpp:(.text+0x5a):对“ChargeTexte(char*,std::vector&)”的未定义引用
main.cpp:(.text+0x66):对“lignelapulslongue(std::vector const&)”的未定义引用
如何修复它?如果只执行
g++main.cpp-o out.o
,则只会生成main.cpp
。要包含另一个.cpp文件,请执行g++main.cpp inoutfich.cpp-o out.o
和平。您的职能定义在哪里?您应该针对包含定义的库进行链接。这是链接器问题。。。您需要链接到定义这些函数的库或对象。请尝试
$g++main.cpp inoutfich.cpp-o out.o
@jsantander:“您需要链接到定义这些函数的库或对象”,这是#include“inoutfich.h”的目的吗
您需要将main.cpp和inoutfich.cpp编译并链接在一起。如果库文件中已经包含inoutfich.cpp,则针对该文件进行链接。正如其他人提到的那样,out.o
至少是可执行文件的一个非常不寻常的名称!没错,out(不带任何扩展名)是Linux的最佳选择,如果在Windows中,他应该使用.exe。我通常会将我的可执行文件命名为主.cpp文件(example.cpp),而不带扩展名。举个例子,就是这样。
/tmp/ccunCT7N.o: In function `main':
main.cpp:(.text+0x5a): undefined reference to `ChargeTexte(char*, std::vector<std::string, std::allocator<std::string> >&)'
main.cpp:(.text+0x66): undefined reference to `LigneLaPlusLongue(std::vector<std::string, std::allocator<std::string> > const&)'