如何为许多源和标题正确组织Cmake文件?

时间:2019-06-07 11:21:04

标签: c++ cmake

我知道这个问题已经问过很多次了。我已经检查了它们,但是我是CMake的新手。因此,我不知道如何解决该问题。

我的文件组织是这样的

e0
|-CMakeLists.txt
|-grade.cpp
|-median.cpp
|-student_info.cpp
|-main.cpp
|-include
  |-grade.h
  |-median.h
  |-student_info.h

我的CMakeLists.txt文件如下所示。

cmake_minimum_required (VERSION 2.6)

project(E0)

set(SOURCE_FILES main.cpp)
include_directories(headers)
add_executable(E0 main.cpp)
target_link_libraries(E0 headers)

运行make时,我已经面朝上

/usr/bin/ld: cannot find -lheaders
collect2: error: ld returned 1 exit status
CMakeFiles/E0.dir/build.make:94: recipe for target 'E0' failed make[2]: *** 
[E0] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/E0.dir/all' 
failed
make[1]: *** [CMakeFiles/E0.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

您能帮我如何编写CMakeLists.txt文件吗?

1 个答案:

答案 0 :(得分:0)

看起来像您的CMakeLists.txt应该看起来像这样:

cmake_minimum_required (VERSION 2.6)

project(E0)

set(SOURCE_FILES main.cpp grade.cpp median.cpp student_info.cpp)
include_directories(include)
add_executable(E0 ${SOURCE_FILES})

您确实应该阅读文档,而不是将随机语句放入文件中。