从C文件创建库时出错

mar*_*arc 1 c c++

我试图从C++代码调用sample_c.c中定义的C函数(带有sample_c.h中的函数声明).我在sample_c.h中使用了这个声明

extern "C" void print_c(void);
Run Code Online (Sandbox Code Playgroud)

和sample_c.c中的这个定义

extern void print_c(void) {....}
Run Code Online (Sandbox Code Playgroud)

并尝试生成我想与我的cpp代码链接的lib.我试图用这个C代码生成lib时遇到错误.

gcc -c sample_c.c
error: expected identifier or ‘(’ before string constant
Run Code Online (Sandbox Code Playgroud)

我无法纠正这个问题.有人可以建议我哪里出错了.

Pet*_*ker 5

extern "C"在C中无效.要编写可用于C和C++的标头,必须确保extern "C"在编译为C时不可见:

#ifdef __cplusplus
extern "C"
#endif
void print_c(void);
Run Code Online (Sandbox Code Playgroud)