首页 > 解决方案 > CMake 构建失败,因为找不到 STL 库

问题描述

我正在尝试在 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

我有这个声明

#include <algorithm> 

另外,在我拥有的一行中:使用命名空间std,IDE说使用无法解析类型

我认为 cmake 有点以不正确的方式混合 .c 和 .cpp 文件。

标签: androidc++ccmake

解决方案


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

它之所以有效g++,是因为那是一个 C++ 编译器。请注意,g++ 会将.c文件视为 C++(它对.cxxand的处理方式.cpp)。CMake 不这样做是因为C 和 C++ 之间存在许多不同的不兼容性


推荐阅读