简体   繁体   English

链接器错误:未定义的引用c ++

[英]linker error: undefined reference c++

I've tried looking at similar problems, but could not easily find one that helped my problem. 我试过看类似的问题,但是找不到能解决问题的问题。

I've created a project in C++ and am working on UNIX to compile, link, and run it. 我用C ++创建了一个项目,我正在UNIX上编译,链接和运行它。 My specific problem is an undefined reference to a method I declare in a separate file. 我的具体问题是对我在单独文件中声明的方法的未定义引用。

In the file SharedCache.cpp, I have the following method: 在SharedCache.cpp文件中,我有以下方法:

int SharedCache::replaceLine(ullong address){
    int evictPID = -1;

    int cacheSet = calcCacheSet( address );
    //random uniformly-distributed value for cache line
    float numLines = static_cast<float>(CACHE_LINES_PER_SET);
    uint cacheLine = static_cast<uint>(uniformDistr( numLines ));

    if(cache[cacheSet][cacheLine] != NULL){
        evictPID = cache[cacheSet][cacheLine]->PID;
    }

    uint PID= calcPID(address);
    uint tag= calcTag(address);

    cache[cacheSet][cacheLine]->setLine(PID, tag); //mutex method

    return PID;
}

The line uint cacheLine = static_cast<uint>( uniformDistr( numLines )); uint cacheLine = static_cast<uint>( uniformDistr( numLines )); makes a call to the function I want to use from another file. 从另一个文件调用我想要使用的函数。 The linker error is an undefined reference to this method. 链接器错误是对此方法的未定义引用。

uniformDistr( float ); uniformDistr(float); is declared in the header distributions.h and defined in distributions.cpp. 在头文件distributions.h中声明并在distributions.cpp中定义。 In my project options I set the linker flag -distributions and I also compiled the distributions.cpp to make sure a distributions.o file exists to link with. 在我的项目选项中,我设置了链接器标志-distributions,我还编译了distributions.cpp,以确保存在要链接的distributions.o文件。

From here, I don't know where to go, because this has not solved the problem. 从这里开始,我不知道去哪里,因为这还没有解决问题。

Without more precise information on which compiler/linker commands were invoked and the exact error outputs, it is difficult to provide a good answer. 如果没有关于调用哪个编译器/链接器命令以及确切错误输出的更准确信息,则很难提供一个好的答案。

However, from your description of what you did, it seems that you are not passing distributions.o to the linker. 但是,根据您对所做内容的描述,您似乎没有将distributions.o传递给链接器。 Unlike other languages where the compiler/linker search for object files to link in automatically, C++ linkers require an explicit specification of the objects to link together. 与编译器/链接器搜索要自动链接的目标文件的其他语言不同,C ++链接器需要明确指定要链接在一起的对象。

Your use of -ldistributions here is incorrect: the -l flag is used to link to a static or dynamic library (respectively .a and .so files on Linux), whereas you want to specify another object file that the linker should consider. 您在此处使用-ldistributions是不正确的:-l标志用于链接到静态或动态 (分别是Linux上的.a和.so文件),而您希望指定链接器应考虑的另一个目标文件 Specifying -ldistributions makes the linker look for distributions.a or distributions.so in the standard library locations. 指定-ldistributions使链接器在标准库位置中查找distributions.adistributions.so

Basically, your linker invocation now looks something like this (probably with many more flags and libraries): 基本上,您的链接器调用现在看起来像这样(可能有更多的标志和库):

gcc -o my_program SharedCache.o -ldistributions

To correctly link the distributions code in, you need to make it look something like (again, many flags and libraries probably missing compared to the real thing): 要正确链接发行版代码,你需要让它看起来像(再次,许多标志和库可能与真实的东西相比):

gcc -o my_program SharedCache.o distributions.o

This should resolve the missing symbols issue and produce a working binary (or at the very least a different error ;-) ). 这应该解决丢失的符号问题并产生一个工作二进制(或至少是一个不同的错误;-))。 How to do this in KDevelop however I do not know. 如何在KDevelop中这样做,但我不知道。

I've not used KDevelop, however, on the command line you would just add distributions.o as an input file to the linking process. 我没有使用KDevelop,但是,在命令行中你只需要将distributions.o作为输入文件添加到链接过程中。 No need for dashes or leaving off the .o extension. 不需要短划线或取消.o扩展名。

Alternatively, can you just add distributions.cpp to your KDevelop project? 或者,您可以将distributions.cpp添加到您的KDevelop项目中吗? That way it should get compiled and linked automatically (this is how it works in things like Visual Studio or Eclipse). 这样它应该自动编译和链接(这是它在Visual Studio或Eclipse之类的工作方式)。

Did you add the distributions.cpp to your makefile? 你有没有在你的makefile中添加distributions.cpp? Also I believe the required linker flag is -ldistributions . 另外我相信所需的链接器标志是-ldistributions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM