Why am I getting a linker error: undefined reference to ...?
Why am I getting a linker error: undefined reference to ...?
为什么我会收到此错误:undefined reference to `bfopen(char const*, int)'
?
我创建了一个共享库,其中包含函数 bfopen
等的函数定义。这个库的名称是 libfiles.so
,它位于我正在构建的本地 ext/lib
目录中。
我正在编译:
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I$(PWD)/src -I$(PWD)/ext/include -o parser_tst parser_tst.cc $(PWD)/src/parser.o -L$(PWD)/ext/lib -lpthread -lfftw3f -ldsp -lfiles
我已经用 $(PWD)
代替了我的工作目录。我用 nm -D ext/lib/libfiles.so
查看了 libfiles.so
并能够找到那里列出的函数定义,所以我不知所措。我也没有收到任何类似 file does not exist: libfiles.so
的错误,所以它似乎至少与该文件链接。
这是我的设置示例:(所有路径都被裁剪到它们的本地路径)
ext/sample/src/test.h:
#ifndef TEST_H
#define TEST_H
int test();
#endif /* TEST_H */
ext/sample/src/test.c:
#include "test.h"
int test() {
return 0;
}
这是清理后的文件结构:
.
./ext
./ext/Makefile
./ext/sample
./ext/sample/src
./ext/sample/src/Makefile
./ext/sample/src/test.c
./ext/sample/src/test.h
./ext/sample/tst
./ext/sample/tst/Makefile
./ext/sample/Makefile
./ext/sample/ext
./src
./tst
./tst/test_tst.cc
./tst/Makefile
./Makefile
在调用 make build
之后它看起来像这样:
.
./ext
./ext/Makefile
./ext/sample
./ext/sample/src
./ext/sample/src/Makefile
./ext/sample/src/test.c
./ext/sample/src/test.h
./ext/sample/src/test.o
./ext/sample/tst
./ext/sample/tst/Makefile
./ext/sample/Makefile
./ext/sample/ext
./ext/sample/include
./ext/sample/include/test.h
./ext/sample/lib
./ext/sample/lib/libtest.so
./ext/lib
./ext/lib/libtest.so
./ext/include
./ext/include/test.h
./src
./tst
./tst/test_tst.cc
./tst/Makefile
./Makefile
最后我调用 make test
产生输出(包括错误):
make -i -C ./tst
make[1]: Entering directory `$(PWD)/tst'
make all_tst
make[2]: Entering directory `$(PWD)/tst'
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I./src -I$(PWD)/ext/include -o test_tst test_tst.cc -L$(PWD)/ext/lib -ltest
test_tst.cc: In function ‘int main(int, char**)’:
test_tst.cc:4:6: warning: unused variable ‘t’ [-Wunused-variable]
int t = test();
^
/tmp/ccMq869q.o: In function `main':
$(PWD)/tst/test_tst.cc:4: undefined reference to `test()'
collect2: error: ld returned 1 exit status
make[2]: [test_tst] Error 1 (ignored)
make[2]: Leaving directory `$(PWD)/tst'
make[1]: Leaving directory `$(PWD)/tst'
我尝试使用 export
(setenv LD_LIBRARY_PATH .
) 的 tcsh 等效项,但没有帮助
一个可能的问题是,您有 ext/sample/src/test.c
个 C-file,它将编译为 C-code。
但是使用你的库的应用程序是 C++ 代码
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I./src -I$(PWD)/ext/include -o test_tst test_tst.cc -L$(PWD)/ext/lib -ltest
并包含一个 header,在这种情况下,它也从 c++ 文件编译为 C++ 代码,但从您的 C-file!
编译为 C-code
所以你应该从 c++ 代码中包含你的 header:
extern "C" {
#include "test.h"
}
如何混合使用 C 和 C++ 代码:mix C and C++ code
链接器无法从 c-file 中找到符号的原因是,C++ 中的名称是 "demangled",因为它需要 "trick" 来进行函数重载。可以在这里找到更好更详细的解释:name mangling
为什么我会收到此错误:undefined reference to `bfopen(char const*, int)'
?
我创建了一个共享库,其中包含函数 bfopen
等的函数定义。这个库的名称是 libfiles.so
,它位于我正在构建的本地 ext/lib
目录中。
我正在编译:
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I$(PWD)/src -I$(PWD)/ext/include -o parser_tst parser_tst.cc $(PWD)/src/parser.o -L$(PWD)/ext/lib -lpthread -lfftw3f -ldsp -lfiles
我已经用 $(PWD)
代替了我的工作目录。我用 nm -D ext/lib/libfiles.so
查看了 libfiles.so
并能够找到那里列出的函数定义,所以我不知所措。我也没有收到任何类似 file does not exist: libfiles.so
的错误,所以它似乎至少与该文件链接。
这是我的设置示例:(所有路径都被裁剪到它们的本地路径)
ext/sample/src/test.h:
#ifndef TEST_H
#define TEST_H
int test();
#endif /* TEST_H */
ext/sample/src/test.c:
#include "test.h"
int test() {
return 0;
}
这是清理后的文件结构:
.
./ext
./ext/Makefile
./ext/sample
./ext/sample/src
./ext/sample/src/Makefile
./ext/sample/src/test.c
./ext/sample/src/test.h
./ext/sample/tst
./ext/sample/tst/Makefile
./ext/sample/Makefile
./ext/sample/ext
./src
./tst
./tst/test_tst.cc
./tst/Makefile
./Makefile
在调用 make build
之后它看起来像这样:
.
./ext
./ext/Makefile
./ext/sample
./ext/sample/src
./ext/sample/src/Makefile
./ext/sample/src/test.c
./ext/sample/src/test.h
./ext/sample/src/test.o
./ext/sample/tst
./ext/sample/tst/Makefile
./ext/sample/Makefile
./ext/sample/ext
./ext/sample/include
./ext/sample/include/test.h
./ext/sample/lib
./ext/sample/lib/libtest.so
./ext/lib
./ext/lib/libtest.so
./ext/include
./ext/include/test.h
./src
./tst
./tst/test_tst.cc
./tst/Makefile
./Makefile
最后我调用 make test
产生输出(包括错误):
make -i -C ./tst
make[1]: Entering directory `$(PWD)/tst'
make all_tst
make[2]: Entering directory `$(PWD)/tst'
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I./src -I$(PWD)/ext/include -o test_tst test_tst.cc -L$(PWD)/ext/lib -ltest
test_tst.cc: In function ‘int main(int, char**)’:
test_tst.cc:4:6: warning: unused variable ‘t’ [-Wunused-variable]
int t = test();
^
/tmp/ccMq869q.o: In function `main':
$(PWD)/tst/test_tst.cc:4: undefined reference to `test()'
collect2: error: ld returned 1 exit status
make[2]: [test_tst] Error 1 (ignored)
make[2]: Leaving directory `$(PWD)/tst'
make[1]: Leaving directory `$(PWD)/tst'
我尝试使用 export
(setenv LD_LIBRARY_PATH .
) 的 tcsh 等效项,但没有帮助
一个可能的问题是,您有 ext/sample/src/test.c
个 C-file,它将编译为 C-code。
但是使用你的库的应用程序是 C++ 代码
g++ -std=c++11 -fPIC -g -Wall -D_DEBUG -I./src -I$(PWD)/ext/include -o test_tst test_tst.cc -L$(PWD)/ext/lib -ltest
并包含一个 header,在这种情况下,它也从 c++ 文件编译为 C++ 代码,但从您的 C-file!
编译为 C-code所以你应该从 c++ 代码中包含你的 header:
extern "C" {
#include "test.h"
}
如何混合使用 C 和 C++ 代码:mix C and C++ code
链接器无法从 c-file 中找到符号的原因是,C++ 中的名称是 "demangled",因为它需要 "trick" 来进行函数重载。可以在这里找到更好更详细的解释:name mangling