首页 > 代码库 > cmake
cmake
helloworld依赖lib1和lib2,主目录包括src
lib1包括hello.h和hello.cpp
lib2包括mymath.h和mymath.cpp
src下包括main.cpp和demo文件夹
demo包括demo.h和demo.cpp
如何组织makefile文件?
[xiaoju@vm-076 test_cmake]$ ls -lCRa helloworld/
helloworld/:
. .. CMakeLists.txt lib src
helloworld/lib:
. .. lib1 lib2
helloworld/lib/lib1:
. .. CMakeLists.txt src
helloworld/lib/lib1/src:
. .. hello.cpp hello.h
helloworld/lib/lib2:
. .. CMakeLists.txt src
helloworld/lib/lib2/src:
. .. mymath.cpp mymath.h
helloworld/src:
. .. demo main.cpp
helloworld/src/demo:
. .. demo.cpp demo.h
(1)对于lib1
[xiaoju@vm-076 lib]$ ls -lCRa lib1
lib1:
. .. CMakeLists.txt src
lib1/src:
. .. hello.cpp hello.h
编写CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | cmake_minimum_required (VERSION 2.6) project (lib1) set(LIB_NAME "hello_lib") file(GLOB_RECURSE LIB_SOURCES "src/*.cpp") file(GLOB_RECURSE LIB_HEADERS "src/*.h") set(ROOT $ {CMAKE_CURRENT_SOURCE_DIR} / .. / .. / ) set(CMAKE_CXX_FLAGS "-g -O0 -pipe -W -Wall -fPIC") set(PROJECT_DIR $ {CMAKE_CURRENT_SOURCE_DIR}) message($ {PROJECT_DIR}) add_library($ {LIB_NAME} STATIC $ {LIB_SOURCES}) #set_target_properties(${LIB_NAME} PROPERTIES OUTPUT_NAME "xxx") # make install install(TARGETS $ {LIB_NAME} DESTINATION $ {PROJECT_DIR} / output / lib) install(FILES $ {LIB_HEADERS} DESTINATION $ {PROJECT_DIR} / output / include) |
首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile
然后执行make生产lib文件,
最后执行make install将include和lib文件拷贝至output目录下
1 2 3 4 5 6 7 8 9 10 | cd lib1 mkdir build cd build cmake ../ make make install # remove tmp files rm -rf build rm -rf output |
(2)对于lib2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | cmake_minimum_required (VERSION 2.6) project (lib2) set(LIB_NAME "mymath_lib") file(GLOB_RECURSE LIB_SOURCES "src/*.cpp") file(GLOB_RECURSE LIB_HEADERS "src/*.h") set(ROOT $ {CMAKE_CURRENT_SOURCE_DIR} / .. / .. / ) set(CMAKE_CXX_FLAGS "-g -O0 -pipe -W -Wall -fPIC") set(PROJECT_DIR $ {CMAKE_CURRENT_SOURCE_DIR}) message($ {PROJECT_DIR}) add_library($ {LIB_NAME} STATIC $ {LIB_SOURCES}) #set_target_properties(${LIB_NAME} PROPERTIES OUTPUT_NAME "xxx") # make install install(TARGETS $ {LIB_NAME} DESTINATION $ {PROJECT_DIR} / output / lib) install(FILES $ {LIB_HEADERS} DESTINATION $ {PROJECT_DIR} / output / include) |
首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile
然后执行make生产lib文件,
最后执行make install将include和lib文件拷贝至output目录下
1 2 3 4 5 6 7 8 9 10 | cd lib1 mkdir build cd build cmake ../ make make install # remove tmp files rm -rf build rm -rf output |
3)对于main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <stdio.h> // lib1 #include "hello.h" // lib2 #include "mymath.h" // src/demo #include "demo.h" using namespace std; int main(int argc, char *argv[]) { const char *name = "world"; hello(name); int result = add(4, 5); printf("%d\n", result); printf("%d\n", demo_square(10)); return 0; } |
首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile
然后执行make生产exe文件,
最后执行make install将exe文件拷贝至bin目录下
最后进入bin目录执行exe文件
1 2 3 4 5 6 7 8 9 | cd helloworld mkdir build cd build cmake ../ make make install cd helloworld/bin ./hello_exe |
cmake