cmake

之前一般是手写Makefile,但是随着工程量的增大,Makefile真是心有余而力不足,如何能把自己从Makefile的琐碎细节中解放出来,专注于应用设计本身,于是研究了一下cmake的CMakeList.txt的写法,下面这个CMakeList.txt中,对于使用到了boost库的项目很有帮助。

#BOOST
#./bootstrap.sh --prefix=/opt/boost/
#./b2 install


cmake_minimum_required(VERSION 2.8)

project(mqtt_dispatcher)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wno-unused-variable")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-O0 -g0")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_C_FLAGS_RELEASE "-O0 -g0")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")

set(BOOST_ROOT "/opt/boost")

#set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
#set(Boost_USE_STATIC_RUNTIME OFF)
# 
#BOOST组件依赖
find_package(Boost REQUIRED COMPONENTS
#  regex
filesystem
system
thread
)

if(NOT Boost_FOUND)
  message("未发现Boost")
endif()

include_directories(${Boost_INCLUDE_DIRS}
#  /opt/boost/lib
 )

FIND_LIBRARY(LIB_MQTT NAMES mosquitto PATHS /usr/lib/ /usr/local/lib/) 
FIND_LIBRARY(LIB_BOOST NAMES mosquitto PATHS /opt/ ) 

find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
  target_compile_options(PUBLIC my_app "-pthread")
endif()

file(GLOB srcfiles "*.cpp")
file(GLOB headerfiles "*.h")

AUX_SOURCE_DIRECTORY(. DIR_SRCS)

ADD_EXECUTABLE(mqtt_dispatcher ${DIR_SRCS})
INCLUDE_DIRECTORIES(/opt/boost/include/)
TARGET_LINK_LIBRARIES(mqtt_dispatcher ${LIB_MQTT} ${LIB_BOOST} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})