简体   繁体   English

使用gcc在C中链接C ++静态库

[英]Linking C++ static Library in C using gcc

In the following code, I am trying to call a dummy function written in C++ (using C++ header files like ap_fixed.h, ap_int.h) from a C function. 在下面的代码中,我试图从C函数调用用C ++编写的伪函数(使用ap_fixed.h,ap_int.h之类的C ++头文件)。 The code runs fine when I compile with g++. 使用g ++编译时,代码运行良好。 But when I use gcc for compiling test.c, it throws an error because I have included a C++ header file which is a valid error. 但是,当我使用gcc编译test.c时,它将引发错误,因为我包含了一个C ++头文件,这是一个有效的错误。

Is there any workaround to compile using gcc? 有使用gcc进行编译的解决方法吗? I have read from some posts that it is not a good practice to merge C/C++ code in this manner. 我从一些帖子中了解到,以这种方式合并C / C ++代码不是一个好习惯。 Please enlighten me if there are any serious repurcussions of working with a large C codebase and doing similar stuff. 如果有使用大型C代码库并进行类似操作的任何严重目的,请给我启发。

Thanks 谢谢

Header File: testcplusplus.h 头文件:testcplusplus.h

#include "ap_fixed.h"
#include "ap_int.h"

#ifdef __cplusplus
extern "C" {
#endif

void print_cplusplus();

#ifdef __cplusplus
}
#endif

testcplusplus.cc testcplusplus.cc

#include <iostream>
#include "testcplusplus.h"

void print_cplusplus() {

ap_ufixed<10, 5,AP_RND_INF,AP_SAT > Var1 = 22.96875; 
std::cout << Var1 << std::endl;
}

test.c 测试

#include <stdio.h>
#include "testcplusplus.h"

int main() {
print_cplusplus();
}

Commands Used: 使用的命令:

g++ -c -o testcplusplus.o testcplusplus.cc 
ar rvs libtest.a testcplusplus.o
gcc -o test test.c -L. -ltest

Error: 错误:

In file included from ap_fixed.h:21:0,
                 from testcplusplus.h:1,
                 from test.c:2:
ap_int.h:21:2: error: #error C++ is required to include this header file

The problem here is that the C++ header ap_fixed.h is included from the C program test.c (indirectly via testcplusplus.h). 这里的问题是C程序test.c中包含了C ++头ap_fixed.h(间接通过testcplusplus.h)。

The solution is to remove the include of headers "ap_fixed.h" and "ap_int.h" from testcplusplus.h and include them directly from testcplusplus.cpp. 解决方案是从testcplusplus.h中删除标头“ ap_fixed.h”和“ ap_int.h”的包含,并直接从testcplusplus.cpp中包含它们。 The C program doesn't need to know about these anyway, only the C++ wrapper uses them directly. 无论如何,C程序不需要知道这些,只有C ++包装器直接使用它们。

In a larger example it might be appropriate to split testcplusplus.h into two headers: one that contains only declarations of the external interface you are presenting to the C environment, and another containing the rest - declarations needed internally in the C++ implementation and any required includes. 在更大的示例中,将testcplusplus.h拆分为两个标头可能是合适的:一个标头仅包含要提供给C环境的外部接口的声明,另一个标头包含其余部分-C ++实现内部所需的声明以及任何所需的声明包含。

Once you have done this, you will still face linking errors because the executable that is produced will contain references to symbols from the C++ runtime libraries, plus any other libraries that your C++ code uses. 完成此操作后,您仍然会遇到链接错误,因为生成的可执行文件将包含对C ++运行时库以及C ++代码使用的任何其他库中符号的引用。 To solve this, add -l directives when compiling the final executable, eg: 为了解决这个问题,在编译最终可执行文件时添加-l指令,例如:

gcc -o test test.c -L. -ltest -lstdc++

You do not need to include ap_int.h and ap_fixed.h at this point, as the declaration of the print_cplusplus function does not need those definitions. 此时您不需要包括ap_int.hap_fixed.h ,因为print_cplusplus函数的声明不需要这些定义。

Rather, include them in testcplusplus.c , so the C compiler can only see the C compatible interface of the C++ code. 而是将它们包含在testcplusplus.c中 ,因此C编译器只能看到C ++代码的C兼容接口。

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

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