Gcc 如何编译混合C和C++;密码? 我有C++类,我想包含在C代码中,所以我写了一个包装: #ifndef __PMPC_CMPC__ #define __PMPC_CMPC__ #ifdef __cplusplus extern "C" #endif void init_motor(); #ifdef __cplusplus extern "C" #endif double* calc_cont_sig(double * ); #endif

Gcc 如何编译混合C和C++;密码? 我有C++类,我想包含在C代码中,所以我写了一个包装: #ifndef __PMPC_CMPC__ #define __PMPC_CMPC__ #ifdef __cplusplus extern "C" #endif void init_motor(); #ifdef __cplusplus extern "C" #endif double* calc_cont_sig(double * ); #endif,gcc,compiler-construction,makefile,Gcc,Compiler Construction,Makefile,这是一个简单的测试 #include "cmpc_ekf.h" #include <stdio.h> int main(int argc, char* argv[]) { init_motor(); } 但是我得到了以下错误(这只是错误代码中的一个标记) cmpc_ekf.cpp:(.text+0x40):未定义对“std::cout”的引用 CMPCYEKF.CPP:(.text +0x45):未定义的引用“STD::Basic ToSooS&STD::运算符< P>。在

这是一个简单的测试

#include "cmpc_ekf.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
    init_motor();
}
但是我得到了以下错误(这只是错误代码中的一个标记)

cmpc_ekf.cpp:(.text+0x40):未定义对“std::cout”的引用

CMPCYEKF.CPP:(.text +0x45):未定义的引用“STD::Basic ToSooS&STD::运算符< P>。在C++应用程序中包含C代码是很简单的(正如您在使用G++构建二进制文件时发现的)。然而,我们并不真正支持走另一条路。我不会说这是不可能的,但是你的“C”代码必须链接到C++运行时,然后初始化C++运行时空间。稍后,它也必须终止C++运行时。我想不出有什么理由马上就这么做。相反,只需在C代码中创建应用程序,C++代码的运行时间是C++运行时的子集。 我要澄清的是,所有这些都是用C++编译器编译C代码。它为您解决了上述所有问题

all : main.o libcmpc_ekf.a
    gcc $(EFLAGS) $(CFLAGS) -static  -o main.out main.o  -L. -lcmpc_ekf

main.o: libcmpc_ekf.a
    gcc $(EFLAGS) $(CFLAGS) -static  -c main.c -L. -lcmpc_ekf

libcmpc_ekf.a: cmpc_ekf.o
    ar  rcs libcmpc_ekf.a  cmpc_ekf.o

cmpc_ekf.o: 
    g++ $(EFLAGS) $(CPPFLAGS) -c cmpc_ekf.cpp

clean:
    rm *.o main.out 
cmpc_ekf.cpp:(.text+0x40): undefined reference to `std::cout'
cmpc_ekf.cpp:(.text+0x45): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'