cmakefile 基础篇
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmakefile 基础篇相关的知识,希望对你有一定的参考价值。
c++ 11
使用c++11 ,使用CMAKE_CXX_STANDARD 11,假定有两个文件main.cc和util.cc
cmake_minimum_required(VERSION 2.8.0)
project(test)
set(CMAKE_CXX_STANDARD 11)
#message(${CMAKE_CURRENT_SOURCE_DIR}/src)
message("strange")
set(SRC ${PROJECT_SOURCE_DIR}/main.cc
${PROJECT_SOURCE_DIR}/util.cc
)
add_executable(t1 ${SRC})
指定编译模式
cmake_minimum_required(VERSION 2.8)
project(TestProject)
#使用C++11
add_compile_options(-std=c++11)
#编译时自动打开MOC
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#编译模式
#SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_BUILD_TYPE "Release")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
然后,执行cmake
cmake -DCMAKE_BUILD_TYPE=Debug/Release + CMakeLists.txt所在路径
最后,执行
make
使用库
以使用opencv库作为例子
cmake_minimum_required(VERSION 2.8.0)
project(test3)
set(OpenCV_DIR "/usr/local/opencv/")
#find_package(OpenCV)
set(CMAKE_CXX_STANDARD 14)
#set(CMAKE_CXXFLAGS += -std=c++11)
include_directories( ${OpenCV_DIR}include/opencv4/)
link_directories(
${OpenCV_DIR}/lib64/
)
message("strange")
set(SRC ${PROJECT_SOURCE_DIR}/main.cc
)
add_executable(t3 ${SRC})
target_link_libraries(t3
libopencv_core.so
libopencv_highgui.so
libopencv_imgcodecs.so
)
动态静态库
cmake_minimum_required(VERSION 2.8.12.2)
project(StreamResolver)
set(CMAKE_BUILD_TYPE “Debug”)
#头文件
include_directories(Include)
include_directories(Source)
#设置编译参数
set(CMAKE_CXX_FLAGS “-ffunction-sections -fdata-sections -Wall -std=c++11 -DPOSIX -DDEBUG_”)
#导入库
add_library(Infra STATIC IMPORTED)
set_target_properties(Infra PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/Lib/libInfra.a)
#设置可执行文件的输出路径(EXCUTABLE_OUTPUT_PATH是全局变量)
set(EXECUTABLE_OUTPUT_PATH ./)
#生成静态库
add_library(StreamResolver
Src/rtp/rtp_demux.cpp
Src/rtp/rtp_header.cpp
Src/ps/ps_demux.cpp
Src/h26x/H264ParseSPS.cpp
Src/h26x/H265ParseSPS.cpp
Src/h26x/h26x_parser.cpp
Src/h26x/MediaBuffer.cpp)
##生成动态库
#add_library(StreamResolver_S SHARED
#Src/rtp/rtp_demux.cpp
#Src/rtp/rtp_header.cpp
#Src/ps/ps_demux.cpp)
test.c
#include<stdio.h>
#include <ucontext.h>
#include <unistd.h>
int main(int argc, const char *argv[]){
ucontext_t context;
getcontext(&context);
puts("Hello world");
sleep(1);
setcontext(&context);
return 0;
}
以上是关于cmakefile 基础篇的主要内容,如果未能解决你的问题,请参考以下文章