cmake-01-basic

本文介绍CMake的基础用法,包括设置最小版本、定义项目名称、添加可执行文件、指定语言类型、创建静态库和共享库等核心功能。还介绍了如何使用目标包含目录和链接库来组织大型项目。

project Basic

	# a basic CMakeLists.txt
	
	# set minimum cmake version
	cmake_minimum_required(VERSION 3.5)
	# set project name
	project(hello-world)
	#elf file will be hello_elf, sourcefile should be hello-world.c
	add_executable(hello_elf hello-world.c)
	# cmake_minimum_required could be used as below

	# FATAL_ERROR report
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(hello-world)
	add_executable(hello_elf hello-world.c)

project()

	# project cmd variable set
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(hello-world)
	# project(hello-world) cmd sets PROJECT_NAME as "hello-world": elf will be "hello-world"
	add_executable(${PROJECT_NAME} hello-world.c)

LANGUAGES

	
	#default language
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	# default language *.c as CC
	project(hello-world)
	add_executable(hello-world hello-world.c)
	#clc language
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	# specify clc
	project(recipe-01 LANGUAGES C)
	add_executable(hello-world hello-world.c)
	# c++ language
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	
	# specify c++ language compiler
	project(hello-world LANGUAGES CXX)
	add_executable(hello-world hello-world.cpp)
	# fortran language
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	# specify fortran language
	project(hello-world LANGUAGES Fortran)
	add_executable(hello-world hello-world.f90)

target_include_directories

	# variable set  and headers 
	
	cmake_minimum_required(VERSION 3.5)
	project (hello_headers)
	# Create a sources variable with a link to all cpp files to compile
	set(SOURCES
	    src/Hello.cpp
	    src/main.cpp
	)
	
	# Add an executable with the above sources
	add_executable(hello_headers ${SOURCES})
	
	# Set the directories that should be included in the build command for this target
	# when running g++ these will be included as -I/directory/path/
	target_include_directories(hello_headers
	    PRIVATE
	        ${PROJECT_SOURCE_DIR}/include
	)

target_link_libraries

	# a basic static lib
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(hello-world LANGUAGES CXX)
	# generate a library from sources , it is a static lib --- "libmessage.a"
	add_library(message
	  STATIC
	    Message.hpp
	    Message.cpp
	  )
	add_executable(hello-world hello-world.cpp)
	#link hello-world.o to "libmessage.a"
	target_link_libraries(hello-world message)
	# using private public
	
	cmake_minimum_required(VERSION 3.5)
	project(hello_library)
	#Generate the static library from the library sources --- "libhello_library.a"
	add_library(hello_library STATIC
	    src/Hello.cpp
	)
	#libhello_library.a or any elf that links to libhello_library.a automatically set -I ${PROJECT_SOURCE_DIR}/include
	target_include_directories(hello_library
	    PUBLIC
	        ${PROJECT_SOURCE_DIR}/include
	)
	
	add_executable(hello_binary
	    src/main.cpp
	)

	# link the new hello_library target with the hello_binary target
	target_link_libraries( hello_binary
	    PRIVATE
	        hello_library
	)
	# a basic shared lib
	
	cmake_minimum_required(VERSION 3.5)
	project(hello_library)
	#Generate the shared library from the library sources, that is --- "libhello_library.so"
	add_library(hello_library SHARED
	    src/Hello.cpp
	)
	# name hello::library is the same as hello_library
	add_library(hello::library ALIAS hello_library)
	
	target_include_directories(hello_library
	    PUBLIC
	        ${PROJECT_SOURCE_DIR}/include
	)
	add_executable(hello_binary
	    src/main.cpp
	)
	# link the new hello_library target with the hello_binary target
	target_link_libraries( hello_binary
	    PRIVATE
	        hello::library
	)
	# object lib, used for build lib
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(hello-world LANGUAGES CXX)
	
	# generate an object library from sources
	add_library(message-objs
	  OBJECT
	    Message.hpp
	    Message.cpp
	  )
	
	# this is only needed for older compilers
	# but doesn't hurt either to have it
	set_target_properties(message-objs
	  PROPERTIES
	    POSITION_INDEPENDENT_CODE 1
	  )
	# obj lib -> shared lib "libmessage.so"
	add_library(message-shared
	  SHARED
	    $<TARGET_OBJECTS:message-objs>
	  )
	set_target_properties(message-shared
	  PROPERTIES
	    OUTPUT_NAME "message"
	  )
	# obj lib -> static lib "libmessage.a"
	add_library(message-static
	  STATIC
	    $<TARGET_OBJECTS:message-objs>
	  )
	set_target_properties(message-static
	  PROPERTIES
	    OUTPUT_NAME "message"
	  )
	
	add_executable(hello-world hello-world.cpp)
	#link to target , not lib name
	target_link_libraries(hello-world message-static)
	# a fotran shared lib
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(hello-world LANGUAGES Fortran)
	# libmessage.so
	add_library(message
	  SHARED
	    message.f90
	  )
	add_executable(hello-world hello-world.f90)
	target_link_libraries(hello-world message)

add_subdirectory

cmake_minimum_required (VERSION 3.5)

project(subprojects)

# Add sub directories
add_subdirectory(sublibrary1)
add_subdirectory(sublibrary2)
add_subdirectory(subbinary)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值