【问题标题】:CMake fails in build because cannot find STL libsCMake 构建失败,因为找不到 STL 库
【发布时间】: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 文件
  • algorithmC++ 标头,不能在 C 代码中使用。
  • 奇怪的是,我可以在项目之外成功编译和运行相同的代码,只使用一个使用 g++ 的 compile.sh 文件。但我会尝试将 .c 更改为 .cpp
  • CMake单独编译每个源文件;对于.cpp,它使用g++ 编译器,但对于.c 文件,它使用gcc。如果你的源文件需要 g++ 编译器,那么它包含 C++ 代码,并且应该有相应的扩展名。

标签: android c++ c cmake


【解决方案1】:

您在 C 源文件 (.c) 中包含 C++ 标头。你不能指望gcc(Cmake 在遇到.c 时调用的C 源代码编译器)能够理解C++。

它适用于g++,因为那是一个 C++ 编译器。请注意,g++ 会将 .c 文件视为 C++(对 .cxx.cpp 的处理方式)。 CMake 不这样做是因为there are many different incompatibilities between C and C++

【讨论】:

    猜你喜欢
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2016-01-14
    • 2015-12-08
    • 2013-06-21
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多