【发布时间】:2019-01-10 19:31:22
【问题描述】:
我正在尝试在 Android Studio 项目中使用 .c 和 .cpp 文件,我已经使用我包含的所有文件配置了 CMakeList。
我的 CMakeList 是这样的:
file(GLOB SOURCES "src/main/cpp/B/*.cpp")
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
src/main/cpp/native-lib.cpp
src/main/cpp/A/B/src/a.c
src/main/cpp/A/B/src/b.c
src/main/cpp/A/B/src/c.c
src/main/cpp/A/B/src/d.c
src/main/cpp/A/a.cpp
src/main/cpp/A/B/src/e.c
src/main/cpp/B/a.cpp
${SOURCES})
考虑到我有这样的目录:
+--- /cpp
| +--- /A
| | +--- /B
| | | +--- /include
| | | | +-- *.h
| | | +--- /src
| | | +-- *.c
| | |
| | |
| +--- /B
| | +--- /include
| | | +-- *.h
| | +--- /src
| | +-- *.cpp
当我运行项目时,我得到了这个
../include/a.h:68:10: fatal error: 'algorithm' file not found
在 a.h 我有这个声明
#include <algorithm>
另外,在我拥有的一行中:使用命名空间 std,IDE 说 using 无法解析类型
我认为 cmake 有点以不正确的方式混合 .c 和 .cpp 文件。
【问题讨论】:
-
收到错误消息时正在编译哪个文件?
-
所有 .c 文件,包括 a.h 文件
-
algorithm是 C++ 标头,不能在 C 代码中使用。 -
奇怪的是,我可以在项目之外成功编译和运行相同的代码,只使用一个使用 g++ 的 compile.sh 文件。但我会尝试将 .c 更改为 .cpp
-
CMake单独编译每个源文件;对于
.cpp,它使用g++ 编译器,但对于.c文件,它使用gcc。如果你的源文件需要 g++ 编译器,那么它包含 C++ 代码,并且应该有相应的扩展名。