C++ 对已定义函数的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4130434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:36:17  来源:igfitidea点击:

C++ undefined reference to defined function

c++g++undefined-reference

提问by Mashew

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.

我无法弄清楚为什么这不起作用。我会放上我的所有三个文件,可能有人可以告诉我为什么会抛出这个错误。我正在使用 g++ 来编译程序。

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

采纳答案by Mud

The declaration and definition of insertLikeare different

的声明和定义insertLike不同

In your header file:

在你的头文件中:

void insertLike(const char sentence[], const int lengthTo, const int length,const char writeTo[]);

void insertLike(const char sentence[], const int lengthTo, const int length,const char writeTo[]);

In your 'function file':

在您的“功能文件”中:

void insertLike(const char sentence[], const int lengthTo, const int length,char writeTo[]);

void insertLike(const char sentence[], const int lengthTo, const int length,字符写入[]);

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.

C++ 允许函数重载,你可以有多个同名的函数/方法,只要它们有不同的参数。参数类型是函数签名的一部分。

In this case, insertLikewhich takes const char*as its fourth parameter and insertLikewhich takes char *as its fourth parameter are different functions.

在这种情况下,insertLike哪个const char*作为其第四个参数和insertLike哪个char *作为其第四个参数是不同的函数

回答by MattK

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.

尽管以前的海报涵盖了您的特定错误,但如果您不告诉编译器使用 C 链接,则在尝试使用 g++ 编译 C 代码时,您可能会收到“未定义引用”链接器错误。

For example you should do this in your C header files:

例如,您应该在 C 头文件中执行此操作:

extern "C" {

...

void myfunc(int param);

...

}

To make 'myfunc' available in C++ programs.

使“myfunc”在 C++ 程序中可用。

If you still also want to use this from C, wrap the extern "C" {and }in #ifdef __cpluspluspreprocessor conditionals, like

如果您还想从 C 中使用它,请将extern "C" {and包装}#ifdef __cplusplus预处理器条件中,例如

#ifdef __cplusplus
extern "C" {
#endif

This way, the externblock will just be “skipped” when using a C compiler.

这样,extern当使用 C 编译器时,块将被“跳过”。

回答by casablanca

You need to compile and link all your source files together:

您需要将所有源文件编译并链接在一起:

g++ main.c function_file.c

回答by AxeEffect

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 cppfiles are correct- if you have not modified CMakeLists.txtaccordingly.

如果您使用 CMake,这也可能发生。如果您创建了一个新类并且想要实例化它,则在构造函数调用时您将收到此错误 - 即使标题和cpp文件是正确的 - 如果您没有进行CMakeLists.txt相应的修改。

With CMake, every time you create a new class, before using it the header, the cppfiles and any other compilable files (like Qt uifiles) must be added to CMakeLists.txtand then re-run cmake .where CMakeLists.txtis stored.

使用 CMake,每次创建新类时,在使用它之前,必须将头cpp文件、文件和任何其他可编译文件(如 Qtui文件)添加到存储位置CMakeLists.txt,然后重新运行。cmake .CMakeLists.txt

For example, in this CMakeLists.txtfile:

例如,在这个CMakeLists.txt文件中:

cmake_minimum_required(VERSION 2.8.11)

project(yourProject)

file(GLOB ImageFeatureDetector_SRC *.h *.cpp)

### Add your new files here ###
add_executable(yourProject YourNewClass.h YourNewClass.cpp otherNewFile.ui})

target_link_libraries(imagefeaturedetector ${SomeLibs})

If you are using the command file(GLOB yourProject_SRC *.h *.cpp)then you just need to re-run cmake .without modifying CMakeLists.txt.

如果您正在使用该命令,file(GLOB yourProject_SRC *.h *.cpp)那么您只需要重新运行cmake .而无需修改CMakeLists.txt.

回答by Paul

If you are including a library which depends on another library, then the order of inclusion is also important:

如果您包含一个依赖于另一个库的库,那么包含的顺序也很重要:

g++ -o MyApp MyMain.o -lMyLib1 -lMyLib2

In this case, it is okay if MyLib1 depends on MyLib2. However, if there reverse is true, you will get undefined references.

在这种情况下,如果 MyLib1 依赖于 MyLib2 就可以了。但是,如果 reverse 为真,您将获得未定义的引用。

回答by MountainLogic

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).

正如保罗所说,这可能是链接器投诉,而不是编译器错误。如果您仔细阅读构建输出/日志(可能需要在单独的 IDE 窗口中查看完整详细信息),您可以确定问题是来自编译器(需要在代码中修复)还是来自链接器(并且需要在 make/cmake/project 级别修复以包含缺少的库)。