Linux下编写CMake

cmake_minimum_required(VERSION 3.10)
set(PNAME sdk)

option(BuildTest "build test" ON)
SET(CMAKE_CXX_COMPILER "g++")     #设置编译器(可选的有gcc,g++)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) #设置输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

#set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -g -pthread -static-libstdc++" )
#set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -O3 -pthread -static-libstdc++" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -g -pthread -static-libstdc++" )


project(${PNAME})
#gtk
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
message(STATUS "----- GTK3_INCLUDE_DIRS: ${GTK3_INCLUDE_DIRS}")
message(STATUS "----- GKT3_LIBRARIES: ${GTK3_LIBRARIES}")
message(STATUS "----- GTK3_LINK_LIBRARIES: ${GTK3_LINK_LIBRARIES}")
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
list(APPEND FC_DEP_LIBS ${GTK3_LIBRARIES})

#opencv 
find_package(OpenCV REQUIRED)
message("opencv dir = ${OpenCV_INCLUDE_DIRS}")
message("Opencv lib = ${OpenCV_LIBRARIES}")
include_directories(${OpenCV_INCLUDE_DIRS})

set(JSON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdPart/jsoncpp)
file(GLOB json_src ${JSON_DIR}/*cpp)
file(GLOB json_head ${JSON_DIR}/json/*.h)
source_group("JSON" FILES ${json_head} ${json_src})
include_directories(${JSON_DIR})

file(GLOB src src/*.cpp src/*.c)
file(GLOB head src/*.h src/*.hpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdPart/spdlog/
                    ${CMAKE_CURRENT_SOURCE_DIR}/3rdPart/
                    ${JSON_DIR}/json
                    ${CMAKE_CURRENT_SOURCE_DIR}/src)
##link_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdPart//lib/aarch64_linux_gnu/)#设置动态链接库路径
add_library(${PNAME} SHARED ${src} ${head} ${json_src} ${json_head})
target_link_libraries(${PNAME} ${OpenCV_LIBRARIES} udev )
#set_target_properties(${PNAME} PROPERTIES  -DMAX_DLL_EXPORT)
#target_link_libraries(${PNAME} megface)

message("test project")

if (BuildTest)
    set(AirPortTest "demo")
    file(GLOB test_src test/*.cpp test/*.c)
    file(GLOB test_head test/*.h test/*.hpp)
    find_package(OpenCV REQUIRED)
    message("opencv dir = ${OpenCV_INCLUDE_DIRS}")
    message("Opencv lib = ${OpenCV_LIBRARIES}")
    include_directories(${OpenCV_INCLUDE_DIRS})
    add_executable(${AirPortTest} ${test_src} ${test_head})
    target_include_directories(${AirPortTest} PRIVATE 
                                            ${CMAKE_CURRENT_SOURCE_DIR}/src 
                                            ${CMAKE_CURRENT_SOURCE_DIR}/3rdPart/spdlog/include
                                            ${CMAKE_CURRENT_SOURCE_DIR}/src/include)
    target_link_libraries(${AirPortTest} ${PNAME}  pthread ${OpenCV_LIBRARIES})
    set(pro_dir ${CMAKE_SOURCE_DIR}/build)                          
endif()

上一篇:Linux下cmake编译单个源文件或多个源文件


下一篇:Native开发工具之静态库和动态库(二,小码农也有大梦想