简体   繁体   English

g ++表示引用未定义,即使`nm`列出了我的目标文件中的符号定义

[英]g++ says references are undefined even though `nm` lists the symbol definitions in my object files

When the Makefile for a project I'm working on hits this line: 当我正在处理的项目的Makefile出现以下行时:

g++ -g build_complex.o simplex.o simplex_src.o split.o permutation_src.o permutation_parity.o tropmod.o main.o -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -L/usr/lib/sagemath/local/lib/ -ligraph -lm -o cpptest

I get the following error: 我收到以下错误:

main.o: In function `main':
/home/xander/Desktop/tropicalmoduli/main.cpp:5: undefined reference to `(anonymous namespace)::ConfigSpace::ConfigSpace(int, int)'
/home/xander/Desktop/tropicalmoduli/main.cpp:9: undefined reference to `(anonymous namespace)::PermWrapper::PermWrapper(permutation*)'
/home/xander/Desktop/tropicalmoduli/main.cpp:11: undefined reference to `(anonymous namespace)::ConfigSpace::getTraceOfPerm((anonymous namespace)::PermWrapper)'
collect2: error: ld returned 1 exit status

Those are functions that are defined in the file tropmod.cpp, which was compiled to tropmod.o. 这些是在tropmod.cpp文件中定义的函数,该文件已编译为tropmod.o。 When I run nm tropmod.o spits out: 当我运行nm tropmod.o吐出:

                 U __cxa_atexit
                 U __dso_handle
                 U free
                 U _GLOBAL_OFFSET_TABLE_
000000000000037c t _GLOBAL__sub_I_tropmod.cpp
                 U igraph_destroy
                 U malloc
                 U _Z10split_freeP5split
                 U _Z12simplex_freeP7simplex
                 U _Z13simplex_traceP7simplexP11permutation
                 U _Z14get_1_skeletoniiPPP5split
                 U _Z16get_mid_skeletoniiiPiPP5splitP8igraph_s
0000000000000333 t _Z41__static_initialization_and_destruction_0ii
00000000000001f0 t _ZN12_GLOBAL__N_111ConfigSpace14getTraceOfPermENS_11PermWrapperE
000000000000010c t _ZN12_GLOBAL__N_111ConfigSpace7destroyEv
0000000000000000 t _ZN12_GLOBAL__N_111ConfigSpaceC2Eii
0000000000000322 t _ZN12_GLOBAL__N_111PermWrapper14getPermutationEv
0000000000000304 t _ZN12_GLOBAL__N_111PermWrapperC2EP11permutation
                 U _ZNSt8ios_base4InitC1Ev
                 U _ZNSt8ios_base4InitD1Ev
0000000000000000 b _ZStL8__ioinit

All the "undefined references" are listed here, with a "t", which should mean that they're defined in this object file. 此处列出了所有“未定义的引用”,并带有“ t”,这意味着它们已在此目标文件中定义。 Both tropmod.o and main.o were compiled from .cpp files which include the header file tropmod.hpp , where the declarations can be found: tropmod.o和main.o都是从.cpp文件编译的,其中包括头文件tropmod.hpp ,可以在其中找到声明:

#include <iostream>
#include <string>
#include "build_complex.h"

#ifdef tmboost
#include <boost/python/list.hpp>
#include <boost/python/extract.hpp>
typedef boost::python::list bplist;
#endif

namespace {

    // allows a permutation to be converted from python::boost::list
    // to permutation* just once, and then reused
    class PermWrapper {
        permutation *perm;

        public:
        #ifdef tmboost 
        PermWrapper(bplist);
        #endif
        PermWrapper(permutation*);

        permutation *getPermutation();
    };

    class ConfigSpace {
        int n,d;
        int *num_cells;
        int num_facets;
        split **all_splits;
        simplex ***skels;

        public:
        ConfigSpace(int n, int d);

        void destroy();

        int getTraceOfPerm(PermWrapper perm);
    };
}

For completeness, here is main.cpp : 为了完整main.cpp ,这里是main.cpp

#include "tropmod.hpp"

int main() {

    ConfigSpace cs = ConfigSpace(2, 2);

    int p_d[4] = {1, 0, 3, 2};
    permutation *p = perm_alloc_data(4, p_d);
    PermWrapper pw = PermWrapper(p);

    printf("trace: %i\n", cs.getTraceOfPerm(pw));
}

Both cpp files were compiled with g++ -g -fPIC -c (and without the tmboost flag). 这两个cpp文件都是使用g++ -g -fPIC -c编译的(没有tmboost标志)。

I looked at several other questions on here concerning this kind of linking error, but none of the answers I read are obviously applicable here. 我在这里查看了有关这种链接错误的其他几个问题,但是我读到的答案显然都不适用于这里。 It seems like all the pieces are there, does anyone have an idea why the linker can't put it together? 似乎所有组件都在那里,有谁知道为什么链接器无法将其放在一起?

In C++, anonymous namespaces are local to their translation unit , and they cannot be referenced from other translation units. 在C ++中, 匿名名称空间是其翻译单元的本地名称 ,并且不能从其他翻译单元引用它们。

If you read nm 's documentation, it clearly states that lowercase letters specify local symbol types. 如果您阅读nm的文档,它会明确指出小写字母指定本地符号类型。

If these were global symbols, they would be marked as "T", and not "t". 如果这些是全局符号,则将其标记为“ T”,而不是“ t”。

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

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