Makefile自动生成:cmake

http://blog.csdn.net/pipisorry/article/details/51647073

编辑makefile文件CMakeLists.txt,使用cmake命令自动生成makefile文件。

一个示例

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(bgs)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

IF(UNIX)
  # add some standard warnings
  ADD_DEFINITIONS(-Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas)
 
  # -ansi does not compile with sjn module
  #ADD_DEFINITIONS(-ansi)

  # if you like to have warinings about conversions, e.g. double->int or double->float etc., or float compare
  #ADD_DEFINITIONS(-Wconversion -Wfloat-equal)
endif(UNIX)

find_package(OpenCV REQUIRED)

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

if(${OpenCV_VERSION} VERSION_EQUAL 3 OR ${OpenCV_VERSION} VERSION_GREATER 3)
  message (FATAL_ERROR "OpenCV version is not compatible: ${OpenCV_VERSION}")
endif()

if(${OpenCV_VERSION} VERSION_LESS 2.3.1)
  message (FATAL_ERROR "OpenCV version is not compatible: ${OpenCV_VERSION}")
endif()

file(GLOB sources FrameProcessor.cpp PreProcessor.cpp VideoAnalysis.cpp  VideoCapture.cpp)
file(GLOB main Main.cpp)
file(GLOB demo Demo.cpp)
file(GLOB demo2 Demo2.cpp)

list(REMOVE_ITEM sources ${demo} ${demo2})

file(GLOB_RECURSE analysis package_analysis/*.cpp)
file(GLOB_RECURSE bgs package_bgs/*.cpp package_bgs/*.c)
file(GLOB_RECURSE bgs_include package_bgs/*.h)

# GMG is not available in older OpenCV versions
if(${OpenCV_VERSION} VERSION_LESS 2.4.3)
  file(GLOB gmg package_bgs/GMG.cpp)
  list(REMOVE_ITEM bgs ${gmg})
endif()

include_directories(${CMAKE_SOURCE_DIR})

add_library(bgs SHARED ${sources} ${bgs} ${analysis})
target_link_libraries(bgs ${OpenCV_LIBS})
set_property(TARGET bgs PROPERTY PUBLIC_HEADER ${bgs_include})

add_executable(bgs_bin ${main})
target_link_libraries(bgs_bin ${OpenCV_LIBS} bgs)
set_target_properties(bgs_bin
  PROPERTIES OUTPUT_NAME bgs)

add_executable(bgs_demo ${demo})
target_link_libraries(bgs_demo ${OpenCV_LIBS} bgs)

add_executable(bgs_demo2 ${demo2})
target_link_libraries(bgs_demo2 ${OpenCV_LIBS} bgs)

INSTALL(TARGETS bgs
    bgs_bin
  RUNTIME DESTINATION bin COMPONENT app
  LIBRARY DESTINATION lib COMPONENT runtime
  ARCHIVE DESTINATION lib COMPONENT runtime
  PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
  FRAMEWORK DESTINATION "/Library/Frameworks"
)

皮皮blog



CMakeLists.txt内容解析

定义变量

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99"),如set(HADOOP_HOME "/usr/local/hadoop-2.6.4"),当然更好的方式是直接引用系统变量。

makefile中引用系统环境变量的方法

set(HADOOP_HOME $ENV{HADOOP_HOME})

添加要生成.so文件的源文件

file(GLOB_RECURSE bgs /opt/GraphLite/GraphLite-0.20/GibbsGraphliteExample/example/PageRankVertex.cc)

cmake时输出相关信息

message(STATUS "    GRAPHLITE_HOME: ${GRAPHLITE_HOME}")

将.cc源文件中include的头文件添加进来

file(GLOB_RECURSE bgs_include ${GRAPHLITE_HOME}/include/*.h ${HADOOP_HOME}/include/*.h)
include_directories(${CMAKE_SOURCE_DIR} ${GRAPHLITE_HOME}/include ${HADOOP_HOME}/include)

最后生成的.so库文件名字就是libGIBBS(前面自动添加lib)

add_library(GIBBS SHARED ${bgs})

皮皮blog


生成库文件*.so

要在CMakeLists.txt同级目录下新建一个build目录,进入并运行

build$cmake ..

没有错误则会在build目录下生成libGIBBS.so文件

这样是没有错误:(warning暂时可以不用管)

/opt/GraphLite/GraphLite-0.20/GibbsGraphliteExample/example/GIBBS.cc:342:75: warning: deleting object of polymorphic class type ...

Linking CXX shared library libGIBBS.so
[100%] Built target GIBBS

出现下面的error就说明出错了

make[2]: *** [CMakeFiles/GIBBS.dir/GIBBS.cc.o] Error 1
make[1]: *** [CMakeFiles/GIBBS.dir/all] Error 2
make: *** [all] Error 2

成功后就可以调用.so文件了

如:pika:/opt/GraphLite/GraphLite-0.20$start-graphlite GibbsGraphliteExample/example/build/libGIBBS.so Input/KCore-graph0_4w Output/out 6
from: http://blog.csdn.net/pipisorry/article/details/51647073

ref: CMake3.6.0-rc1 Documentation

cmake-commands(7)

cmake 常用变量和常用环境变量查表手册---整理


你可能感兴趣的:(C++)