一.单个文件的编译
1.目录结构
在当前目录下新建一个build目录,
1.mkdir build
2.cd build
3.cmake ../
4.make
ps:在build目录下make clean可以去除调生成hello可执行文件。
danny@llp:build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- can't detect runtime and arch
--
--
-- /home/danny/share/CMake/01SimpleExample/build
--
--
-- /home/danny/share/CMake/01SimpleExample
CMake Warning at CMakeLists.txt:10 (message):
/home/danny/share/CMake/01SimpleExample/CMakeLists.txt
-- /home/danny/share/CMake/01SimpleExample
-- 3
-- 5
-- 1
-- Linux-4.15.0-142-generic
-- Linux
-- 4.15.0-142-generic
-- x86_64
-- Configuring done
-- Generating done
-- Build files have been written to: /home/danny/share/CMake/01SimpleExample/build
helloworld.cpp
#include <iostream>
int main()
{
std::cout << "hello world cmake!!!"<< std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(hello)
message(STATUS "can't detect runtime and arch")
message(STATUS ${project_source_dir})
message(STATUS ${project_binary_dir})
message(STATUS ${CMAKE_CURRENT_BINARY_DIR})
message(STATUS ${CMAKE_INCLUDE_PATH})
message(STATUS ${CMAKE_LIBRARY_PATH})
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
message(WARNING ${CMAKE_CURRENT_LIST_FILE})
#message(FATAL_ERROR ${CMAKE_CURRENT_LIST_LINE})
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS ${CMAKE_MAJOR_VERSION})
message(STATUS ${CMAKE_MINOR_VERSION})
message(STATUS ${CMAKE_PATCH_VERSION})
message(STATUS ${CMAKE_SYSTEM})
message(STATUS ${CMAKE_SYSTEM_NAME})
message(STATUS ${CMAKE_SYSTEM_VERSION})
message(STATUS ${CMAKE_SYSTEM_PROCESSOR})
AUX_SOURCE_DIRECTORY(. SRC_LIST)
add_executable(hello ${SRC_LIST})
二.多文件代码编译
文件目录如下:
student.h
#include <iostream>
void MyPrint();
student.cpp
#include "student.h"
void MyPrint(){
std::cout << "i am a student " << std::endl;
}
main.cpp
#include "student.h"
int main()
{
MyPrint();
std::cout <<"hello world" << std::endl;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(DirExample)
#Bring the headers ,such as Student.h into the project
include_directories(include)
#Can manually add the sources using the set command as follows:
set(SOURCES src/main.cpp src/student.cpp)
#However, the file(GLOB...) allows for wildcard additions
#file(GLOB SOURCES "src/*.cpp")
add_executable(testStudent ${SOURCES})
三.编译动态库
目录如下:
student.h
#include <iostream>
void MyPlay();
student.cpp
#include "student.h"
void MyPlay(){
std::cout << " this is so lib " << std::endl;
}
CMakeLists.txt
project(buildsolib)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
#Generate the shared library from the sources
add_library(testStudent SHARED ${SOURCES})
#set the location for library installation --i.e.
#/usr/lib in this case
#not really necessary in this example, use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/lib)
运行结果:
danny@llp:03BuildSolib$ mkdir build
danny@llp:03BuildSolib$ ls
build CMakeLists.txt include src
danny@llp:03BuildSolib$ cd build/
danny@llp:build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.5)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/danny/share/CMake/03BuildSolib/build
danny@llp:build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
danny@llp:build$ make
Scanning dependencies of target testStudent
[ 50%] Building CXX object CMakeFiles/testStudent.dir/src/student.o
[100%] Linking CXX shared library libtestStudent.so
[100%] Built target testStudent
danny@llp:build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libtestStudent.so Makefile
danny@llp:build$
四.编译静态库
student.h
#include <iostream>
void MyPlayVideo();
student.cpp
#include "student.h"
void MyPlayVideo(){
std::cout << "this is a test for static lib" << std::endl;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(buildstaticlib)
FIND_LIBRARY(libpath testStudent /usr/lib)
IF (NOT libpath)
MESSAGE(FATAL_ERROR "libtestStudent is not found")
ENDIF(NOT libpath)
message(STATUS ${libpath})
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
#Generate the shared library from the sources
add_library(testStudent STATIC ${SOURCES})
#set the location for library installation --i.e.
#/usr/lib in this case
#not really necessary in this example, use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/lib)
运行结果
danny@llp:04BuildStaticlib$ mkdir build
danny@llp:04BuildStaticlib$ ls
build CMakeLists.txt include src
danny@llp:04BuildStaticlib$ cd build/
danny@llp:build$ ls
danny@llp:build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- /usr/lib/libtestStudent.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/danny/share/CMake/04BuildStaticlib/build
danny@llp:build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
danny@llp:build$ make
Scanning dependencies of target testStudent
[ 50%] Building CXX object CMakeFiles/testStudent.dir/src/student.cpp.o
[100%] Linking CXX static library libtestStudent.a
[100%] Built target testStudent
danny@llp:build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libtestStudent.a Makefile
danny@llp:build$
五.库的使用
libtest.cpp
#include "student.h"
int main()
{
MyPlay();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(testlib)
#For the shared library
set(PROJECT_LINK_LIBS libtestStudent.so)
link_directories(/home/danny/share/CMake/03BuildSolib/build)
#For the static library
#set (PROJECT_LINK_LIBS libtestStudent.a)
#link_directories(~/CMake/04BuildStaticlib/build)
include_directories(/home/danny/share/CMake/03BuildSolib/include)
SET(PROJECT_BINARY_DIR ../)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
message(${PROJECT_LINK_LIBS})
add_executable(libtest libtest.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS})
输出结果
danny@llp:build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- bin output directory:
..//bin
-- Configuring done
-- Generating done
-- Build files have been written to: /home/danny/share/CMake/05Uselib/build
danny@llp:build$ make
Scanning dependencies of target libtest
[ 50%] Building CXX object CMakeFiles/libtest.dir/libtest.cpp.o
[100%] Linking CXX executable ../bin/libtest
[100%] Built target libtest
danny@llp:build$ cd ..
danny@llp:05Uselib$ ls
bin build CMakeLists.txt libtest.cpp
danny@llp:05Uselib$ cd bin
danny@llp:bin$ ls
libtest
danny@llp:bin$ ./libtest
this is so lib