首页 > 其他分享 >Tool-CMake-How CMake simplifies the build process by Bruno Abinader

Tool-CMake-How CMake simplifies the build process by Bruno Abinader

时间:2023-04-29 13:11:38浏览次数:42  
标签:simplifies CMake process helloworld ng itemviews -- build cmake

Tool-CMake-How CMake simplifies the build process by Bruno Abinader

https://gitlab.kitware.com/cmake/community/-/wikis/home

https://brunoabinader.github.io/2009/12/07/how-cmake-simplifies-the-build-process-part-1-basic-build-system/

https://brunoabinader.github.io/2009/12/09/how-cmake-simplifies-the-build-process-part-2-advanced-build-system/


Part 1: Basic build system

https://brunoabinader.github.io/2009/12/07/how-cmake-simplifies-the-build-process-part-1-basic-build-system/

CMake macro MacroOutOfSourceBuild.cmake which requires the user to build the source code outside its base directory, ensuring the developer uses a shadow build directory.

example project

two directories (base directory and src/ directory)

$ find helloworld
helloworld/
helloworld/src
helloworld/src/main.cpp
helloworld/src/helloworld.cpp
helloworld/src/CMakeLists.txt
helloworld/src/helloworld.h
helloworld/cmake
helloworld/cmake/modules
helloworld/cmake/modules/MacroOutOfSourceBuild.cmake
helloworld/CMakeLists.txt

helloworld/CMakeLists.txt (base directory)

# Project name is not mandatory, but you should use it
project(helloworld)

# States that CMake required version must be greater than 2.6
cmake_minimum_required(VERSION 2.6)

# Appends the cmake/modules path inside the MAKE_MODULE_PATH variable which stores the
# directories of additional CMake modules (ie. MacroOutOfSourceBuild.cmake):
set(CMAKE_MODULE_PATH ${helloworld_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})

# The macro below forces the build directory to be different from source directory:
include(MacroOutOfSourceBuild)

macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out of source build.")

add_subdirectory(src)

helloworld/src/CMakeLists.txt

# Include the directory itself as a path to include directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Create a variable called helloworld_SOURCES containing all .cpp files:
set(helloworld_SOURCES helloworld.cpp main.cpp)

# For a large number of source files you can create it in a simpler way
# using file() function:
# file(GLOB hellworld_SOURCES *.cpp)

# Create an executable file called helloworld from sources:
add_executable(helloworld ${helloworld_SOURCES})

create a shadow build directory (i.e. build/)

$ mkdir build
$ cd build
$ cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bruno/helloworld/build

$ make
[ 50%] Building CXX object src/CMakeFiles/helloworld.dir/helloworld.cpp.o
[100%] Building CXX object src/CMakeFiles/helloworld.dir/main.cpp.o
Linking CXX executable helloworld
[100%] Built target helloworld

Part 2:Advanced build system

https://brunoabinader.github.io/2009/12/09/how-cmake-simplifies-the-build-process-part-2-advanced-build-system/

./stc/CMakeFiles.txt

# Installs the header files into the {build_dir}/include/itemviews-ng directory install(FILES ${itemviews-ng_HEADERS} DESTINATION include/itemviews-ng)

# Installs the target file (libitemviews-ng.so) into the {build_dir}/lib directory install(TARGETS itemviews-ng LIBRARY DESTINATION lib)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Create a variable containing a list of all implementation files
file(GLOB itemviews-ng_SOURCES *.cpp)
 
# The same applies for headers which generates MOC files (excluding private implementation ones)
file(GLOB itemviews-ng HEADERS *[^_p].h)
 
# Now the magic happens: The function below is responsible for generating the MOC files)
automoc4_moc_headers(itemviews-ng ${itemviews-ng_HEADERS})
 
# Creates a target itemviews-ng which creates a shard library with the given sources
automoc4_add_library(itemviews-ng SHARED ${itemviews-ng_SOURCES})
 
# Tells the shared library to be linked against Qt ones
target_link_libraries(itemviews-ng ${QT_LIBRARIES})
 
# Installs the header files into the {build_dir}/include/itemviews-ng directory
install(FILES ${itemviews-ng_HEADERS} DESTINATION include/itemviews-ng)
 
# Installs the target file (libitemviews-ng.so) into the {build_dir}/lib directory
install(TARGETS itemviews-ng LIBRARY DESTINATION lib)

标签:simplifies,CMake,process,helloworld,ng,itemviews,--,build,cmake
From: https://www.cnblogs.com/yongchao/p/17363894.html

相关文章

  • ReadAlignChunk_processChunks.cpp:204:processChunks EXITING because of FATAL ERRO
     001、star报错 002、解决方法fastq文件为压缩格式,运行时需添加该参数:--readFilesCommandzcat ......
  • Tool-CMake-A Simple CMake Example
    Tool-CMake-ASimpleCMakeExamplehttps://cmake.org/examples/Therearethreedirectoriesinvolved.Thetopleveldirectoryhastwosubdirectoriescalled./Demoand./Hello.Inthedirectory./Hello,alibraryisbuilt.Inthedirectory./Demo,anexecuta......
  • CMakeLists---自定义变量-add_definitions()函数
    转载:https://blog.csdn.net/qq_35699473/article/details/115837708引言其实这个函数在安装一些库的时候,它的CMakeLists里面就有这样的函数。典型的就是opencv了。opencv安装时候有一些指令也是针对这个函数的,比如安装命令(随便搜索的):cmake ../opencv-3.4.1-DWITH_GTK_2......
  • IDEA从零到精通(21)之使用Maven clean发生错误Process terminated
    IDEA从零到精通(21)之使用Mavenclean发生错误Processterminated原文链接:https://blog.csdn.net/dkm123456/article/details/121871870文章目录作者简介引言导航热门专栏推荐错误描述解决方案:再次clean小结导航热门专栏推荐作者简介作者名:编程界明世隐简介:CSDN博客......
  • CLion远程调试CMake项目
    cmake项目的远程调试。CLion安装在本地Windows系统,cmake项目部署在远程的Linux系统。配置远程调试可以比较方便地进行Linux端的cmake项目开发。点击setting。主要配置这三个:Toolchains点击+号,添加RemoteHost。然后先点击Credentials的设置按钮,添加远程连接。配置SSHCon......
  • java出现class lombok.javac.apt.LombokProcessor错误
    出现:java:java.lang.IllegalAccessError:classlombok.javac.apt.LombokProcessor(inunnamedmodule@0x3278991b)cannotaccessclasscom.sun.tools.javac.processing.JavacProcessingEnvironment(inmodulejdk.compiler)becausemodulejdk.compilerdoesnotexpor......
  • cmake包含单独.hpp文件
    myproject/├──CMakeLists.txt├──main.cpp└──include└──hello.hpp#OpenCVfind_package(OpenCVREQUIRED)include_directories(${OpenCV_INCLUDE_DIRS})#Eigenfind_package(Eigen3REQUIRED)include_directories(${EIGEN3_INCLUDE_DIR})include_dire......
  • [Node.js] Hanlde process.env with dotenv
    import*asdotenvfrom"dotenv";constresult=dotenv.config();if(result.error){console.log('Errorloadingenvironmentvariables,aborting.')process.exit(1)}console.log(process.env.PORT) ......
  • Android之Service设置android:process作用
    原文地址blog.csdn.net在AndroidManifest.xml中定义service时会看到这样的代码android:process=”:remote”,例如:1.<service2.android:3.android:enabled="true"4.android:exported="false"5.android:process=":remote"/&g......
  • python subprocess Popen非阻塞,读取adb日志
    简单版fromthreadingimportThreadfromqueueimportQueue,Emptyimportshlexif__name__=='__main__':print_hi('PyCharm')#Car().run()defenqueue_output(stdout,queue):withopen("www.log",'w......