Cmake学习笔记

发布时间:2023年12月20日

1.linux下静态库包含静态库

使用qt在windows下是已经跑通过,但是在linux下一直没有成功。

测试发现linux可以实现.a连接.a的方案.

测试例子编译libtest1.a 和libtest2.a。libtest2.a中调用了libtest1.a 。最后在main.c中调用libtest2.a的函数。

配置环境变量文件
export XPL_OBJECT_TYPE="LINUX-X86"
export XPL_PROJECT_DIR=$(pwd)
export XPL_FEATURE_DIR=$XPL_PROJECT_DIR/feature
export XPL_PROTOCOL_DIR=$XPL_PROJECT_DIR/protocol
export CCACHE_PATH=/usr/bin:$CCACHE_PATH
export CC="gcc"
export AR="gcc-ar"
export CXX="g++"
export CPP="gcc"
export AS="as"
export GDB=gdb
export STRIP=strip
export RANLIB=ranlib
export OBJCOPY=objcopy
export OBJDUMP=objdump
编译用的脚本文件
#!/bin/bash

echo "=======================Bulid helloworld Test================="

echo "$1"
echo "$2"
echo "$3"

project_path=$(cd `dirname $0`; pwd)
project_name="${project_path##*/}"

echo $project_path
echo $project_name

check_build_dir=`ls -l |grep "build"`

echo $check_build_dir

if [[ $check_build_dir =~ "Build/" ]]
then
	echo "Has bulid Directroy!"
else
	echo "Create Build Directory!"
	mkdir -p Build
fi

#app build
cd $project_path/Build/
cmake -D CMAKE_BUILD_TYPE=$2 ..

if [[ "$1" =~ "clean" ]]
then
	make clean-all
	rm -rf ./*
	#clear swp
	cd $project_path 

	#python version
	python -V
	rtn="$?"
	if [[ $rtn == "0" ]]
	then
		echo "python"
		python cleartmp.py
	else
		echo "python3"
		python3 cleartmp.py
	fi

	echo $check_python_ver
	echo "====================================....clean Success....========================================="
else
	echo "====================================....Build start....==============================================="
	make -j4
	echo "generate helloworld at ./Build/bin/"
	echo "====================================....Build Success....========================================="
	echo "press anykey to running helloworld!"
	read input

	cd $project_path/Build/bin

	./helloworld 
fi

编译静态库libtest1.a
test1.h
/**
 * @brief 测试函数
 * @return int 
 */
int test1();
test1.c
#include <stdio.h>
#include "test.h"

/**
 * @brief 测试函数
 * @return int
 */
int test1()
{
    printf("Hello test 1\n\r");
    return 0;
}
cmakelist文件
 cmake_minimum_required(VERSION 3.0.0)
 #设置可执行文件的名字
 project(test1 VERSION 0.1.0)
 
 #reset output path 设置编译生成文件的存放路径
 set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin )
 set ( LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib )
 
 include(CTest)
 enable_testing()
 
 #set include file 配置头文件路径
 include_directories (./inc)
  
 #set source file  配置源文件路径
 aux_source_directory ( . DIR_USR_SOURCE ) 
 aux_source_directory ( ./src DIR_USR_SOURCE )
 
 
 add_library(${CMAKE_PROJECT_NAME} ${DIR_USR_SOURCE})
 
 set(CPACK_PROJECT_NAME ${PROJECT_NAME})
 set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
 include(CPack)
 
编译静态库libtest2.a

包含了test1的头文件也调用了test1的函数

test2.h
/**
 * @brief 
 * @return int 
 */
int test2();
test2.c
#include <stdio.h>
#include "test2.h"
#include "test.h"
/**
 * @brief test2测试函数
 * @return int 
 */
int test2()
{
    test1();
    printf("Hello test 2\n\r");
    return 0;
}
cmakelist文件

只需要指定test1.h的路径即可编译通过

cmake_minimum_required(VERSION 3.0.0)
#设置可执行文件的名字
project(test2 VERSION 0.1.0)

#reset output path 设置编译生成文件的存放路径
set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin )
set ( LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib )

include(CTest)
enable_testing()

#set include file 配置头文件路径
include_directories (./inc)
# include_directories (${XPL_THIRDPARTY_DIR}/test1/inc)
#注意这里指定test1.h的路径
include_directories (./thirdparty/test1/inc)   

#set source file  配置源文件路径
aux_source_directory ( . DIR_USR_SOURCE ) 
aux_source_directory ( ./src DIR_USR_SOURCE )

add_library(${CMAKE_PROJECT_NAME} ${DIR_USR_SOURCE})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
编译可执行文件
main.cpp
#include <stdio.h>
#include "test.h"
#include "test2.h"

int main()
{
    printf("Hello world\n\r");
    test1();
    test2();
    return 0;
}

cmakelist文件
#cmake version
cmake_minimum_required ( VERSION 3.10 )

project ( helloworld CXX C)



#reset output path
set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin )
set ( LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib )

set ( CMAKE_SHARED_LIBRARY_LINK_C_FLAGS )

message(STATUS "CMake Info")
message(STATUS "=======================================================")
message(STATUS "    Operation System :  ${CMAKE_SYSTEM}")
message(STATUS "    CPU Architecture : ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "    Build Type : ${CMAKE_BUILD_TYPE}${CMAKE_CONFIGURATION_TYPES}")
message(STATUS "    Shared Library  : ${BUILD_SHARED_LIBS}")
message(STATUS "    Build Examples  : ${CSERIALPORT_BUILD_EXAMPLES}")
message(STATUS "    Build Test  : ${CSERIALPORT_BUILD_TEST}")
message(STATUS "=======================================================")
#set include file
include_directories (./inc)

#set source file
aux_source_directory ( . DIR_USR_SOURCE )
# aux_source_directory ( ./src DIR_USR_SOURCE )

#set CC AR LD AS
set ( CMAKE_C_COMPILER $ENV{CC} )
set ( CMAKE_C_AR $ENV{AR} )
set ( CMAKE_C_LD $ENV{LD} )

#CFLAGS
set ( CMAKE_C_FLAGS "-g -std=c++11" )
set ( CMAKE_CXX_FLAGS "-g -std=c++11" )

add_definitions (
	-DELPP_FEATURE_ALL
	-DELPP_FEATURE_CRASH_LOG
	#-DELPP_NO_DEFAULT_LOG_FILE
	-DELPP_THREAD_SAFE
	-DELPP_OS_UNIX
	#-DELPP_SYSLOG
)

#set debug
if(CMAKE_BUILD_TYPE STREQUAL Debug)
	add_definitions (
		-DPRINT_DEBUG_INFO
	)
endif()


#添加静态库
add_library ( libtest2 STATIC IMPORTED )
add_library ( libtest1 STATIC IMPORTED )

# #静态库的路径
set_property (TARGET libtest1 PROPERTY IMPORTED_LOCATION $ENV{XPL_THIRDPARTY_DIR}/test1/libtest1.a)
set_property (TARGET libtest2 PROPERTY IMPORTED_LOCATION  $ENV{XPL_THIRDPARTY_DIR}/test2/libtest2.a)


MESSAGE( STATUS "There are source files : ${DIR_USR_SOURCE}.")
#generate target
add_executable ( ${CMAKE_PROJECT_NAME} ${DIR_USR_SOURCE} )
target_link_libraries ( ${CMAKE_PROJECT_NAME} libtest1 libtest2)  

#add custom
add_custom_target ( clean-all COMMAND rm -rf {CMAKE_BINARY_DIR}/* )

执行结果
root@lkt-VirtualBox:~/gitlab/HelloWorld# 
root@lkt-VirtualBox:~/gitlab/HelloWorld# ./build_project.sh 
=======================Bulid helloworld Test=================
/root/gitlab/HelloWorld
HelloWorld
-rwxr-xr-x 1 root root 1245 127 15:40 build_project.sh
Create Build Directory!
-- CMake Info
-- =======================================================
--     Operation System :  Linux-5.8.0-55-generic
--     CPU Architecture : x86_64
--     Build Type : 
--     Shared Library  : 
--     Build Examples  : 
--     Build Test  : 
-- =======================================================
-- There are source files : ./main.cpp.
-- Configuring done
-- Generating done
-- Build files have been written to: /root/gitlab/HelloWorld/Build
====================================....Build start....===============================================
Consolidate compiler generated dependencies of target helloworld
[100%] Built target helloworld
generate helloworld at ./Build/bin/
====================================....Build Success....=========================================
press anykey to running helloworld!

2.cmake编译如何划分release和debug版本的可执行文件。

releae:体积小。
debug:可以单步调试
文章来源:https://blog.csdn.net/teaczjf/article/details/135099056
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。