当我正在处理的项目的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
我收到以下错误:
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
这些是在tropmod.cpp文件中定义的函数,它被编译为tropmod.o。当我运行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
此处列出了所有“未定义的引用”,带有“t”,这意味着它们已在此目标文件中定义。 tropmod.o和main.o都是从包含头文件tropmod.hpp
的.cpp文件编译的,其中可以找到声明:
#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);
};
}
为了完整性,这里是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));
}
两个cpp文件都使用g++ -g -fPIC -c
进行编译(并且没有tmboost
标志)。
我在这里看了几个关于这种链接错误的其他问题,但我读到的答案中没有一个显然适用于此。似乎所有的部分都在那里,有没有人知道为什么链接器不能把它放在一起?
答案 0 :(得分:2)
在C ++中,anonymous namespaces are local to their translation unit,无法从其他翻译单元引用它们。
如果您阅读nm
的文档,则会明确指出小写字母指定了本地符号类型。
如果这些是全局符号,则它们将被标记为“T”,而不是“t”。