简体   繁体   中英

cmake errors: Cannot find source file

I'm trying to use cmake for the first time and I'm having a hard time getting this to work. There's a source file and a library file ( Lab_4.cpp and Trip_4.cpp , respectively) and they're both in the source folder ( Lab_4 ). Here's what's in my cmake file so far:

cmake_minimum_required (VERSION 2.6)
project (Lab_4)

#add executable
add_executable(Lab_4 ${PROJECT_SOURCE DIR}/Lab_4.cxx)
target_link_libraries (Lab_4 ${EXTRA_LISTS})

#add libraries
add_library (${PROJECT_SOURCE_DIR}/Trip.cxx)

ls shows both files are in that folder. I'm really new to cmake so I'm sure I'm making an obvious mistake but I have no idea what it is. Any help would be appreciated!

To add your sources to CMake project, you can use the aux_source_directory command. If your sources are in Lab_4 folder, then you can do the following:

project(Lab_4)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/Lab_4 LAB4_SOURCES)
add_executable(Lab_4 ${LAB4_SOURCES})

The CMAKE_CURRENT_SOURCE_DIR is the path of the directory where the current CMakeFiles.txt is. The example above won't build Trip_4.cpp as a library but use it as another source file together with Lab_4.cpp .


If you wish to build the Trip_4.cpp as library fist I recommend to separate it from Lab_4.cpp to make later uses easier. An example directory structure could be:

MyProject/
|-- app/
|   |-- src/
|   |   `-- Lab_4.cpp
|   |-- inc/
|   `-- CMakeLists.txt
|-- lib/
|   |-- src/
|   |   `-- Trip_4.cpp
|   |-- inc/
|   `-- CMakeLists.txt
`-- CMakeLists.txt

In this case the MyProject/lib/CMakeLists.txt will contain something like the following:

project(Trip_4)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src TRIP4_SOURCES)

add_library(Trip_4 ${TRIP4_SOURCES})

The MyProject/app/CMakeLists.txt will be almost the same as I showed in my first example, except now it has to maintain its dependency on Trip_4 library:

project(Lab_4)

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/../lib
    ${CMAKE_CURRENT_SOURCE_DIR}/inc
)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src LAB4_SOURCES)

add_executable(Lab_4 ${LAB4_SOURCES})
add_dependencies(Lab_4 Trip_4)
target_link_libraries(Lab_4 Trip_4)

Finally, the MyProject/CMakeLists.txt has to tie together the library and the executable subprojects as follows:

project(MyProject)

add_subdirectory(lib)
add_subdirectory(app)
cmake_minimum_required(VERSION 2.6)
project(Lab_4)

add_library(Trip_4 Lab_4/Trip4.cpp)
add_executable(Lab_4 Lab_4/Lab_4.cpp)
target_link_libraries(Lab_4 Trip_4 ${EXTRA_LISTS})

Your exact intentions are not completely clear, but the above are the simplest possible instructions according to your current question description.

Are you absolutely sure your CMake is version 2.6? You should update the version to whatever CMake you are currently using (type cmake --version to find out).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM