C++对已定义函数的未定义引用
我不明白为什么这不起作用。我将把我的所有三个文件放在一起,也许有人可以告诉我为什么它会抛出这个错误。我正在使用 g++ 来编译该程序。
程序:
#include <iostream>
#include "h8.h"
using namespace std;
int main()
{
char sentence[MAX_SENTENCE_LENGTH];
char writeTo[] = "output.txt";
int distanceTo,likePosition, length, numWords;
cout << "ENTER A SENTENCE! ";
cin.getline(sentence, 299);
length = strlen(sentence);
numWords = wordCount(sentence, length);
for(int x = 0; x < 3; ++x)
{
likePosition = likePos(numWords);
distanceTo = lengthTo(sentence, likePosition, length);
insertLike(sentence, distanceTo, length, writeTo);
}
return 0;
}
函数文件:
void insertLike(const char sentence[], const int lengthTo, const int length, char writeTo[])
{
char part1[MAX_SENTENCE_LENGTH], part2[MAX_SENTENCE_LENGTH];
char like[] = " like ";
for(int y = 0; y < lengthTo; ++y)
part1[y] = sentence[y];
for(int z = lengthTo+1; z < length - lengthTo; ++z)
part2[z] = sentence[z];
strcat(part1, like);
strcat(part1, part2);
writeToFile(sentence, writeTo);
return;
}
头文件:
void insertLike(const char sentence[], const int lengthTo, const int length, const char writeTo[]);
错误确切的是:
undefined reference to 'insertLike(char const*, int, int, char const*)'
collect2: ld returned 1 exit status
I cannot figure out why this is not working. I will put up all three of my files and possibly someone can tell me why it is throwing this error. I am using g++ to compile the program.
Program:
#include <iostream>
#include "h8.h"
using namespace std;
int main()
{
char sentence[MAX_SENTENCE_LENGTH];
char writeTo[] = "output.txt";
int distanceTo,likePosition, length, numWords;
cout << "ENTER A SENTENCE! ";
cin.getline(sentence, 299);
length = strlen(sentence);
numWords = wordCount(sentence, length);
for(int x = 0; x < 3; ++x)
{
likePosition = likePos(numWords);
distanceTo = lengthTo(sentence, likePosition, length);
insertLike(sentence, distanceTo, length, writeTo);
}
return 0;
}
Function file:
void insertLike(const char sentence[], const int lengthTo, const int length, char writeTo[])
{
char part1[MAX_SENTENCE_LENGTH], part2[MAX_SENTENCE_LENGTH];
char like[] = " like ";
for(int y = 0; y < lengthTo; ++y)
part1[y] = sentence[y];
for(int z = lengthTo+1; z < length - lengthTo; ++z)
part2[z] = sentence[z];
strcat(part1, like);
strcat(part1, part2);
writeToFile(sentence, writeTo);
return;
}
Header file:
void insertLike(const char sentence[], const int lengthTo, const int length, const char writeTo[]);
The error exactly is:
undefined reference to 'insertLike(char const*, int, int, char const*)'
collect2: ld returned 1 exit status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
insertLike
的声明和定义具有不同的writeTo
参数。在你的头文件中,你有:
在你的函数文件中:
C++允许函数重载,你可以有多个同名的函数/方法,只要它们有不同的参数。参数类型是函数签名的一部分。
在本例中,
insertLike
采用const char*
作为其第四个参数,insertLike
采用char *
作为其第四个参数第四个参数是不同的功能。The declaration and definition of
insertLike
have differentwriteTo
parameters.In your header file, you have:
while in your function file:
C++ allows function overloading, where you can have multiple functions/methods with the same name, as long as they have different arguments. The argument types are part of the function's signature.
In this case,
insertLike
which takesconst char*
as its fourth parameter andinsertLike
which takeschar *
as its fourth parameter are different functions.尽管以前的海报涵盖了您的特定错误,但如果您不告诉编译器使用 C 链接,则在尝试使用 g++ 编译 C 代码时,您可能会收到“未定义引用”链接器错误。
例如,您应该在 C 头文件中执行此操作:
使“myfunc”在 C++ 程序中可用。
如果您仍然想在 C 中使用它,请将
extern "C" {
和}
包装在#ifdef __cplusplus
预处理器条件中,如下所示这样,当使用 C 编译器时,
extern
块将被“跳过”。Though previous posters covered your particular error, you can get 'Undefined reference' linker errors when attempting to compile C code with g++, if you don't tell the compiler to use C linkage.
For example you should do this in your C header files:
To make 'myfunc' available in C++ programs.
If you still also want to use this from C, wrap the
extern "C" {
and}
in#ifdef __cplusplus
preprocessor conditionals, likeThis way, the
extern
block will just be “skipped” when using a C compiler.您需要编译所有源文件并将其链接在一起:
You need to compile and link all your source files together:
如果您使用 CMake,也可能会发生这种情况。如果您创建了一个新类并且想要实例化它,则在构造函数调用时您将收到此错误 - 即使标头和
cpp
文件正确 - 如果您尚未修改CMakeLists.txt 相应地。
使用 CMake,每次创建新类时,在使用头文件之前,必须将
cpp
文件和任何其他可编译文件(如 Qtui
文件)添加到 < code>CMakeLists.txt,然后重新运行存储CMakeLists.txt
的cmake .
。例如,在此
CMakeLists.txt
文件中:如果您使用命令
file(GLOB yourProject_SRC *.h *.cpp)
那么您只需重新运行 < code>cmake . 而不修改CMakeLists.txt
。This could also happen if you are using CMake. If you have created a new class and you want to instantiate it, at the constructor call you will receive this error -even when the header and the
cpp
files are correct- if you have not modifiedCMakeLists.txt
accordingly.With CMake, every time you create a new class, before using it the header, the
cpp
files and any other compilable files (like Qtui
files) must be added toCMakeLists.txt
and then re-runcmake .
whereCMakeLists.txt
is stored.For example, in this
CMakeLists.txt
file:If you are using the command
file(GLOB yourProject_SRC *.h *.cpp)
then you just need to re-runcmake .
without modifyingCMakeLists.txt
.如果您要包含的库依赖于另一个库,那么包含的顺序也很重要:
在这种情况下,如果 MyLib1 依赖于 MyLib2 也没关系。
但是,如果情况相反,您将得到未定义的引用。
If you are including a library which depends on another library, then the order of inclusion is also important:
In this case, it is okay if MyLib1 depends on MyLib2.
However, if there reverse is true, you will get undefined references.
正如 Paul 所说,这可能是链接器投诉,而不是编译器错误。如果您仔细阅读构建输出/日志(可能需要在单独的 IDE 窗口中查看才能查看完整的详细信息),如果问题来自编译器(需要在代码中修复)或来自链接器(并且需要在 make/cmake/project 级别进行修复以包含缺少的库)。
As Paul said, this can be a linker complaint, rather than a compiler error. If you read your build output/logs carefully (may need to look in a separate IDE window to see the full details) you can dell if the problem is from the compiler (needs to be fixed in code) or from the linker (and need to be fixed in the make/cmake/project level to include a missing lib).