使用gcc连接C++静态库。

时间:2022-09-18 12:21:41

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. The code runs fine when I compile with 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.

在下面的代码中,我尝试调用一个用c++编写的虚拟函数(使用像ap_fixed这样的c++头文件)。h, ap_int.h,从C函数。当我用g++编译时,代码运行良好。但是当我使用gcc进行编译测试时。它抛出一个错误,因为我包含了一个c++头文件,这是一个有效的错误。

Is there any workaround to compile using gcc? I have read from some posts that it is not a good practice to merge C/C++ code in this manner. Please enlighten me if there are any serious repurcussions of working with a large C codebase and doing similar stuff.

是否有使用gcc来编译的变通方法?我从一些帖子中看到,以这种方式合并C/ c++代码并不是一种好的做法。如果有什么严重的问题,请告诉我是否有严重的问题,使用一个大的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

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

2 个解决方案

#1


3  

The problem here is that the C++ header ap_fixed.h is included from the C program test.c (indirectly via testcplusplus.h).

这里的问题是c++头ap_fixed。h包含在C程序测试中。通过testcplusplus.h c(间接)。

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. The C program doesn't need to know about these anyway, only the C++ wrapper uses them directly.

解决方案是删除标题“ap_fixed”。h”和“ap_int。从testcplusplus h”。并直接从testcplusplus.cpp中包含它们。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. To solve this, add -l directives when compiling the final executable, eg:

完成此操作后,仍然会面临链接错误,因为生成的可执行文件将包含对来自c++运行库的符号的引用,以及c++代码使用的任何其他库。要解决这个问题,在编译最终可执行文件时添加-l指令,例如:

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

#2


1  

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。h和ap_fixed。此时,正如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兼容接口。

#1


3  

The problem here is that the C++ header ap_fixed.h is included from the C program test.c (indirectly via testcplusplus.h).

这里的问题是c++头ap_fixed。h包含在C程序测试中。通过testcplusplus.h c(间接)。

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. The C program doesn't need to know about these anyway, only the C++ wrapper uses them directly.

解决方案是删除标题“ap_fixed”。h”和“ap_int。从testcplusplus h”。并直接从testcplusplus.cpp中包含它们。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. To solve this, add -l directives when compiling the final executable, eg:

完成此操作后,仍然会面临链接错误,因为生成的可执行文件将包含对来自c++运行库的符号的引用,以及c++代码使用的任何其他库。要解决这个问题,在编译最终可执行文件时添加-l指令,例如:

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

#2


1  

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。h和ap_fixed。此时,正如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兼容接口。