我有一个非常微不足道的问题,包括将 C 库链添加到 C++ 主项目中。我有使用 C 的经验,但这是我第一次使用 C++ 编程。
项目的结构是一个文件夹,里面有:
- main.cpp
- Mylib_1.c
- Mylib_1.h
- Mylib_2.c
- Mylib_2.h
主要调用 -> 调用的 Mylib_1.h -> My_lib2.h
//main.cpp
#include "Mylib_1.h"
//Mylib_1.h
#include "Mylib_2.h"
main 包含 Mylib_1 和 Mylib_2 函数以及 typedef 结构
Mylib_1 使用 Mylib_2 的 typedef 结构和函数
每个 Mylib_x.h 中的所有内容都包含在 extern "C"之间,如下所示:
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
#ifdef __cplusplus
extern "C" {
#endif
mycode
#ifdef __cplusplus
}
#endif
#endif
但是当我尝试在 Ubuntu 12.04 x64 上使用 eclipse kepler 编译它时,我得到:
Mylib_1.h error: Mylib_2_type_t does not name a type
main.cpp error: Mylib_2_function1 was not declared in this scope
...
只有上述部分在 eclipse 中被标记为错误,标题看起来很好。
此外,根据 eclipse,__cplusplus 标志在 Mylib_2.h 中为假,但在 Mylib_1.h 中为真
考虑到一些 eclipse 错误,我尝试通过 g++ (v4.6.3) 手动构建项目,但是当我尝试将库 .o 与 main.cpp 链接时,我遇到了同样的问题
看起来很愚蠢,但我不知道它会是什么。有什么建议吗?
谢谢
最佳答案
你有没有检查过你的线路
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
这两个文件确实不同, 例如_MYLIB1_H 和 _MYLIB2_H?
关于c++ - C 库链接到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368801/