C 库未使用 gcc/g++ 链接
我有一个在 gcc 中使用的 c 库。 该库的扩展名为 .lib,但始终作为静态库链接。 如果我编写一个使用该库作为 C 代码的程序,一切都会正常。 但是,如果我将文件重命名为 .cpp (执行在 c/c++ 中都有效的简单操作),我会得到未定义的引用。 这些是我为了测试目的而编写的简单小程序,所以没有什么花哨的东西。 我编译使用:
gcc -g -Wall -I <path to custom headers> -o program main.c customlibrary.lib -lm -lpthread
上面的效果就像一个魅力。 但是:
g++ -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread
或
gcc -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread -lstdc++
导致对 customlibrary.lib 中任何函数的未定义引用。 我尝试创建一个名为 customlibrary.a 的符号链接,但没有成功。
为什么 g++ find 无法识别我的库。 不幸的是,我无法访问库的源代码,但将 c-lib 链接到 c++ 应该不是问题,对吗?
I have a c-library which I use in gcc. The library has the extension .lib but is always linked as a static library. If i write a program which uses the library as c-code, everything as a-ok. If I however rename the file to .cpp (doing simple stuff that works in both c/c++) I get undefined reference. These are simple small programs I write for testing purposes so no fancy stuff. I compile using:
gcc -g -Wall -I <path to custom headers> -o program main.c customlibrary.lib -lm -lpthread
The above works like a charm. However:
g++ -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread
or
gcc -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread -lstdc++
results in undefined reference to any function in customlibrary.lib. I tried creating a symbolic link named customlibrary.a but no luck.
Why won't g++ find recognize my library. Unfortunately I have no access to the source code of the libraries but linking a c-lib to c++ should not be a problem right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的库似乎有一个 API,假设它将从 C 而不是 C++ 调用。 这很重要,因为 C++ 实际上要求从库导出的符号包含更多信息,而不仅仅是函数名称。 这是通过“名称修改”函数来处理的。
我假设您的库有一个声明其公共接口的包含文件。 为了使其与 C 和 C++ 兼容,您应该安排告诉 C++ 编译器,它声明的函数应该假定使用 C 的链接和命名。
测试此问题的一个可能的简单答案是这样做:
在 main.cpp 中,而不是直接包含
customlibrary.h
。为了使头文件本身能够在两种语言中工作,并正确地将其函数声明为类似于 C 的 C++ 函数,请将以下内容放在头文件顶部附近:
并将以下内容放在底部附近:
Your library appears to have an API that assumes it will be called from C, not C++. This is important because C++ effectively requires that the symbols exported from a library have more information in them than just the function name. This is handled by "name mangling" the functions.
I assume your library has an include file that declares its public interface. To make it compatible with both C and C++, you should arrange to tell a C++ compiler that the functions it declares should be assumed to use C's linkage and naming.
A likely easy answer to test this is to do this:
in your main.cpp instead of just including
customlibrary.h
directly.To make the header itself work in both languages and correctly declare its functions as C-like to C++, put the following near the top of the header file:
and the following near the bottom:
C++ 编译器执行所谓的名称修改 - 代码中出现的名称与链接器看到的名称不同。 解决这个问题的正常方法是告诉编译器某些函数需要 C 链接:
或者对整个头文件执行此操作:
The C++ compiler performs what is known as name-mangling - the names that appear in your code are not the same ones as your linker sees. The normal way round this is to tell the compiler that certain functions need C linkage:
or do it for a whole header file:
您的头文件是否具有通常
显式给出库函数 C 链接的功能。
.cpp 文件默认使用 C++ 链接(即名称修改)进行编译。
Does your header file have the usual
to give the library functions C linkage explicitly.
.cpp files are compiled with C++ linkage i.e. name mangling by default.