无法使用Cmake构建

时间:2018-03-09 17:05:53

标签: makefile build compiler-errors cmake linker

我是Cmake的新手,我正在尝试创建一个CMakeLists.txt来构建我的项目。我可以使用g ++编译器从命令行构建我的项目,但是当涉及到Cmake时,我很困惑。

目录结构如下:

Project_folder
   |--> Source
       |--> main.cpp
       |--> file1.cpp
       |--> file2.cpp
   |--> Header
       |--> header1.h
       |--> header2.h
   |--> build (from where I run cmake .. and make)
       CMakeLists.txt (this is under Project_folder)
Dependencies
   |--> utils
       |--> Utils.cpp
   |--> include (has many folders in here)
   |--> build (this path is in LD_LIBRARY_PATH as well)
       |--> sharedlib1.so
       |--> sharedlib2.so

现在从Project_Folder中我可以成功运行:

g++ ./Source/main.cpp ./Source/file1.cpp ./Source/file2.cpp ../Dependencies/utils/Utils.cpp -I ../Dependencies -I ../Dependencies/include/ -I ./Header/ -L ../Dependencies/build -std=c++11 -lsharedlib1 -lsharedlib2 -o ./build/main `pkg-config opencv --cflags --libs`

并生成可执行文件。

现在我想配置一个CMakeLists.txt并尝试复制上面的编译器行正在做什么但没有成功(我可以 cmake .. ,但我不能 make )。 CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

project(my_project)

#For the shared library:
set ( PROJECT_LINK_LIBS sharedlib1.so sharedlib2.so)
link_directories(${PROJECT_SOURCE_DIR}/../Dependencies/build/ )

include_directories(${PROJECT_SOURCE_DIR}/../Dependencies ${PROJECT_SOURCE_DIR}/../Dependencies/include ${PROJECT_SOURCE_DIR}/Header)
file(GLOB SOURCES "Source/*.cpp" )

set(CMAKE_CXX_FLAGS_RELEASE pkg-config opencv cflags libs)

## add_executable(name_of_output.o list_of_cpp_files)
add_executable(build ${SOURCES})

生成 makefile 后运行 make 我得到undefined reference到main.cpp内的所有内容。 CMakeLists.txt中有什么明显的东西我应该改变吗?

3 个答案:

答案 0 :(得分:0)

您对g++的来电包含../Dependencies/utils/Utils.cpp文件,但您对add_executable的来电仅使用Source/*.cpp匹配的文件。一个快速的解决方案是将../Dependencies/Utils.cpp添加到调用add_executable()使其成为

add_executable(build ${SOURCES} "${PROJECT_SOURCE_DIR}/../Dependencies/Utils.cpp")

相关说明:CMake不鼓励使用file(GLOB ...)获取源文件列表,通常最好明确列出源文件

答案 1 :(得分:0)

您应该在 add_executable 之后使用 target_link_libraries

请查看示例:

在运行应用程序时,您也会遇到一些错误。请注意RPATH。

答案 2 :(得分:0)

好的,我设法解决了! :)

# set Cmake minimum version to use
cmake_minimum_required(VERSION 2.8.9)

# set the compiler
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# name your project
project(my_project)
set(CMAKE_BUILD_TYPE Release)

# Find and load the settings from the packages
find_package(PkgConfig REQUIRED)
find_package(OpenCV REQUIRED )

# set the extra flags 
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -cflags -libs")

# set the shared libraries name (where the libraries are libsharedlib1.so and libsharedlib2.so + opencv runtime libraries)
set(PROJECT_LINK_LIBS sharedlib1 sharedlib2) ${OpenCV_LIBS}

# include the header files (.h)
include_directories(${PROJECT_SOURCE_DIR}/../Dependencies ${PROJECT_SOURCE_DIR}/../Dependencies/include ${PROJECT_SOURCE_DIR}/Header)

# add the source files (.cpp) using the set command as follow
set(SOURCES
Source/main.cpp
Source/file1.cpp
Source/file2.cpp
${PROJECT_SOURCE_DIR}/../Dependencies/utils/Utils.cpp)

# link the shared libraries (/build/libsharedlib1.so and /build/libsharedlib2.so)
link_directories(${PROJECT_SOURCE_DIR}/../Dependencies/build/)

# add_executable(name_of_output list_of_cpp_files)
add_executable(main ${SOURCES})

# link the executable with the shared libraries
target_link_libraries(main ${PROJECT_LINK_LIBS} )

最后./main将运行程序!