+CMakeLists.txt
|
+--+ src/
| |
| +---- XX.c
| /---- CMakeLists.txt
+--+ libcommon/
| |
| +--- xxx.h
| +--- xxx.c
| /--- CMakeLists.txt
/-- + build
顶层CMakeLists
cmake_minimum_required(VERSION 2.8)
PROJECT(opengl-test C CXX) add_subdirectory(src) add_subdirectory(libcommon) |
add_subdirectory告诉cmake去子目录寻找新的CMakeLists.txt
|
src 的CMakeLists
include_directories(${PROJECT_SOURCE_DIR}/libcommon)
SET(SRCS Simple_Texture2D.c Hello_Triangle.c ) ADD_DEFINITIONS("-DDEBUG") FOREACH(SRC ${SRCS}) get_filename_component(TARGET_NAME ${SRC} NAME_WE) ADD_EXECUTABLE(${TARGET_NAME} ${SRC}) TARGET_LINK_LIBRARIES(${TARGET_NAME} libcommon) ENDFOREACH(SRC) | include_directories将给定的路径添加到编译器搜索包含文件的路径列表中 PROJECT_SOURCE_DIR 指的是工程顶层目录 ADD_DEFINITIONS 供设置-D..等编译预处理需要的宏定义参数
foreach(loop_var arg1 arg2 ...) COMMAND1(ARGS ...) COMMAND2(ARGS ...) ... endforeach(loop_var)
get_filename_component(<VAR> FileName PATH|ABSOLUTE|NAME|EXT|NAME_WE|REALPATH [CACHE])
将变量
<VAR>设置为路径(PATH),文件名(NAME),文件扩展名(EXT),
去掉扩展名的文件名(NAME_WE)
ADD_EXECUTABLE 生成TARGET_NAME为名称的可执行程序 TARGET_LINK_LIBRARIES 将libcommon 链接到 TARGET_NAME这个目标上 |
libcommon的CMakeLists
SET(LIB_SRC esUtil.c
esShapes.c esTransform.c esShader.c) add_library(libcommon ${LIB_SRC}) set_target_properties(libcommon PROPERTIES OUTPUT_NAME "common") TARGET_LINK_LIBRARIES(libcommon -lGLESv2 -lEGL -lm -lX11) | 本来生成的静态库是liblibcommoon.a 为了生成的名称libcommon.a
set_target_properties 修改库的输出名称为 libcommon.a
add_library 生成一个库,类似于add_executable
|