我有一个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
这是一个简单的测试
。#include "cmpc_ekf.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
init_motor();
}
我尝试使用静态库来包含C ++代码,这是我的Makefile:
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&)'
如果我使用
g++
创建二进制文件,那么一切都正常。如果我在创建二进制文件时不使用g++
或-lstdc ++
,如何将C++类或库包含到C代码中?