如何使用CMake使C / C ++混合项目工作

时间:2019-04-05 23:08:18

标签: c++ c cmake compiler-errors

我有一个混合的C / C ++项目,可以成功地使用Makefile进行编译和运行(如果需要,请这样说)。 我想使用CMake构建项目。我的CMakeLists.txt看起来如下

cmake_minimum_required(VERSION 3.13)
project(project_name)

set(CMAKE_CXX_STANDARD 11)

include_directories(include)

add_executable(main
        src/some_file.cpp
        src/other_file.c
        ... and so on
)

我基本上有一个include /和src /文件夹,分别存放C和CPP源文件和头文件。

现在,在一个文件(utils.hpp)中,我加入了string和vector这样的向量。

#include <string>
#include <vector>

尝试构建时,我得到fatal error: 'string' file not found。 相反,我可以#include <string.h>来使错误消失,但是我得到fatal error: 'vector' file not found。非常奇怪的行为。我试图用非常简单的代码

设置一个简单的虚拟项目。
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<int> vec(3);
    std::cout << std::string("Hello, World!") << std::endl;
    return 0;
}

和最简单的CMakeLists.txt都为此文件添加了一个目标,与上面相同,并且一切正常。那么C / C ++的混合是否存在问题?是什么原因导致这些问题?

编译器信息:

Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

编辑:添加请求的信息。
make VERBOSE=Y上从CMake生成的Makefile上的输出。

/usr/local/Cellar/cmake/3.14.1/bin/cmake -S/Users/matthausheer/university/howtowritefastnumericalcode/fastcode -B/Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.14.1/bin/cmake -E cmake_progress_start /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild/CMakeFiles /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/benchmark.dir/build.make CMakeFiles/benchmark.dir/depend
cd /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild && /usr/local/Cellar/cmake/3.14.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/matthausheer/university/howtowritefastnumericalcode/fastcode /Users/matthausheer/university/howtowritefastnumericalcode/fastcode /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/cmakebuild/CMakeFiles/benchmark.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/benchmark.dir/build.make CMakeFiles/benchmark.dir/build
[ 14%] Building C object CMakeFiles/benchmark.dir/src/hgwosca.c.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc  -I/Users/matthausheer/university/howtowritefastnumericalcode/fastcode/include  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk   -o CMakeFiles/benchmark.dir/src/hgwosca.c.o   -c /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/src/hgwosca.c
In file included from /Users/matthausheer/university/howtowritefastnumericalcode/fastcode/src/hgwosca.c:14:
/Users/matthausheer/university/howtowritefastnumericalcode/fastcode/include/utils.hpp:3:10: fatal error: 'string' file not found
#include <string>
         ^~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/benchmark.dir/src/hgwosca.c.o] Error 1
make[1]: *** [CMakeFiles/benchmark.dir/all] Error 2
make: *** [all] Error 2

这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(fastcode)

set(CMAKE_CXX_STANDARD 11)

include_directories(include)

add_executable(benchmark
        src/benchmark.cpp
        src/hgwosca.c
        src/objectives.c
        src/penguin.c
        src/run_benchmark.cpp
        src/utils.cpp)

对于我项目中的以下文件。

enter image description here

0 个答案:

没有答案