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:
我有一个c库,我用在gcc中。该库具有扩展名.lib,但总是以静态库的形式链接。如果我写一个程序用库作为c-code,一切都是a-ok。但是,如果我将文件重命名为.cpp(做一些简单的工作,可以在c/c++中工作),我将得到未定义的引用。这些是我为测试目的编写的简单的小程序,所以没有什么花哨的东西。我编译使用:
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.
结果在自定义库中对任何函数的未定义引用。我尝试创建一个名为customlibrary的符号链接。但没有运气。
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?
为什么g++找不到我的图书馆?不幸的是,我无法访问库的源代码,但是将c-lib链接到c++应该不是问题,对吗?
3 个解决方案
#1
37
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.
您的库似乎有一个API,它假定将从C而不是c++调用它。这很重要,因为c++有效地要求从库导出的符号中包含的信息要比函数名更多。这是通过“名称管理”函数来处理的。
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.
我假设您的库有一个包含文件,该文件声明了它的公共接口。为了使它与C和c++兼容,您应该安排告诉c++编译器,它声明的函数应该假定使用C的链接和命名。
A likely easy answer to test this is to do this:
一个很容易测试的答案是:
extern "C" {
#include "customlibrary.h"
}
in your main.cpp instead of just including customlibrary.h
directly.
在你的主。cpp而不是仅仅包含customlibrary。直接h。
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:
要使头文件本身在两种语言中工作,并正确地将其功能声明为C-like到c++,请将以下内容放在头文件的顶部附近:
#ifdef __cplusplus
extern "C" {
#endif
and the following near the bottom:
下面是下面的内容:
#ifdef __cplusplus
}
#endif
#2
4
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:
c++编译器执行所谓的名称管理——代码中出现的名称与链接器看到的名称不同。通常的方法是告诉编译器某些函数需要C连接:
// myfile.cpp
extern "C" int libfun(); // C function in your library
or do it for a whole header file:
或者对整个头文件这样做:
// myfile.cpp
extern "C" {
#include "mylibdefs.h" // defs for your C library functions
}
#3
2
Does your header file have the usual
你的头文件有通常的吗
#ifdef __cplusplus
extern "C" {
#endif
// ...
#ifdef __cplusplus
} /* extern "C" */
#endif
to give the library functions C linkage explicitly.
要显式地给库函数C链接。
.cpp files are compiled with C++ linkage i.e. name mangling by default.
.cpp文件使用c++链接进行编译,即默认名称管理。
#1
37
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.
您的库似乎有一个API,它假定将从C而不是c++调用它。这很重要,因为c++有效地要求从库导出的符号中包含的信息要比函数名更多。这是通过“名称管理”函数来处理的。
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.
我假设您的库有一个包含文件,该文件声明了它的公共接口。为了使它与C和c++兼容,您应该安排告诉c++编译器,它声明的函数应该假定使用C的链接和命名。
A likely easy answer to test this is to do this:
一个很容易测试的答案是:
extern "C" {
#include "customlibrary.h"
}
in your main.cpp instead of just including customlibrary.h
directly.
在你的主。cpp而不是仅仅包含customlibrary。直接h。
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:
要使头文件本身在两种语言中工作,并正确地将其功能声明为C-like到c++,请将以下内容放在头文件的顶部附近:
#ifdef __cplusplus
extern "C" {
#endif
and the following near the bottom:
下面是下面的内容:
#ifdef __cplusplus
}
#endif
#2
4
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:
c++编译器执行所谓的名称管理——代码中出现的名称与链接器看到的名称不同。通常的方法是告诉编译器某些函数需要C连接:
// myfile.cpp
extern "C" int libfun(); // C function in your library
or do it for a whole header file:
或者对整个头文件这样做:
// myfile.cpp
extern "C" {
#include "mylibdefs.h" // defs for your C library functions
}
#3
2
Does your header file have the usual
你的头文件有通常的吗
#ifdef __cplusplus
extern "C" {
#endif
// ...
#ifdef __cplusplus
} /* extern "C" */
#endif
to give the library functions C linkage explicitly.
要显式地给库函数C链接。
.cpp files are compiled with C++ linkage i.e. name mangling by default.
.cpp文件使用c++链接进行编译,即默认名称管理。