菜鸟笔记
提升您的技术认知

cmake 多级目录-ag真人游戏

阅读 : 190
├── app
│   ├── app1
│   │   ├── app1.c
│   │   ├── app1.h
│   │   └── cmakelists.txt
│   ├── app2
│   │   ├── app2.c
│   │   ├── app2.h
│   │   └── cmakelists.txt
│   ├── app3
│   │   ├── app3.c
│   │   ├── app3.h
│   │   └── cmakelists.txt
│   └── cmakelists.txt
├── cmakelists.txt
├── include
│   └── static.h
├── lib
│   └── libhello_library.a
├── main
│   ├── cmakelists.txt
│   └── main.c
└── public
	├── cmakelists.txt
	├── public1
   	│   ├── cmakelists.txt
	│   ├── public1.c
	│   └── public1.h
	└── public2
    	├── cmakelists.txt
    	├── public2.c
    	└── public2.h

结构说明

app目录存放应用层代码
public中存放公共代码
main中存放主函数代码
lib中存放三方静态库(本节使用cmake系列(三) cmake编译出静态库和动态库并使用产生的静态库)
include存放三方库需要用到的头文件

调用关系

		├──app1.c
		│	
		├──app2.c
		│	└──public2.c
		│	
		├──app3.c
		│	└──libstatic_library.a
main.c──│	
		├──public1.c
		│	
		├──public2.c
		│
		└──libstatic_library.a

源文件

main.c

#include "app1.h"
#include "app2.h"
#include "app3.h"
#include "public1.h"
#include "public2.h"
#include "static.h"
int main(int argc, char *argv[])
{
  
	printf("main: cmake\r\n");
	app1_print();
	app2_print();
	app3_print();
	public1_print();
	public2_print();
	static_print();
	printf("main: end\r\n");
    return 0;
}

app1.c

#include "app1.h"
void app1_print(void)
{
  
    printf("app1: cmake\r\n");
    printf("app1: end\r\n");
}

app2.c

#include "app2.h"
#include "public2.h"
void app2_print(void)
{
  
    printf("app2: cmake\r\n");
    public2_print();
    printf("app2: end\r\n");
}

app3.c

#include "app3.h"
#include "static.h"
void app3_print(void)
{
  
    printf("app3: cmake\r\n");
    static_print();
    printf("app3: end\r\n");
}

public1.c

#include "public1.h"
void public1_print(void)
{
  
    printf("public1: cmake\r\n");
    printf("public1: end\r\n");
}

public2.c

#include "public2.h"
void public2_print(void)
{
  
    printf("public2: cmake\r\n");
    printf("public2: end\r\n");
}

cmakelists.txt

最外层cmakelists

cmake_minimum_required(version 3.5)
project(subprojects)
# add sub directories
# 定义子目录src,用以递归的调用src中的makelists.txt
add_subdirectory(public)
add_subdirectory(app)
add_subdirectory(main)

最后三行,使用add_subdirectory调用子目录的makelists.txt

main的cmakelists

project(main)
# create the executable
add_executable(${
  project_name} main.c)
target_include_directories( ${
  project_name}
    public ${
  subprojects_source_dir}/include
)
#link_directories( ${
  subprojects_source_dir}/lib )
# link the static library from subproject1 using it's alias sub::lib1
# link the header only library from subproject2 using it's alias sub::lib2
# this will cause the include directories for that target to be added to this project
target_link_libraries(${
  project_name}
    lib::app1
    lib::app2
    lib::app3
    lib::public1
    lib::public2
    ${
  subprojects_source_dir}/lib/libstatic_library.a
)

最后一行使用target_link_libraries 连接其他文件。

app的cmakelists

#定义子目录
add_subdirectory(app1)
add_subdirectory(app2)
add_subdirectory(app3)
)

app1的cmakelists

# set the project name
project (app1)
# add a library with the above sources
add_library(${
  project_name} app1.c)
add_library(lib::app1 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
)

app2的cmakelists

# set the project name
project (app2)
# add a library with the above sources
add_library(${
  project_name} app2.c)
add_library(lib::app2 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
)
target_link_libraries(${
  project_name}
    lib::public2
)

app3的cmakelists

# set the project name
project (app3)
# add a library with the above sources
add_library(${
  project_name} app3.c)
add_library(lib::app3 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
    	   ${
  subprojects_source_dir}/include
)
target_link_libraries(${
  project_name}
    ${
  subprojects_source_dir}/lib/libstatic_library.a
)

public的cmakelists

#定义子目录
add_subdirectory(public1)
add_subdirectory(public2)
)

public1的cmakelists

# set the project name
project (public1)
# add a library with the above sources
add_library(${
  project_name} public1.c)
add_library(lib::public1 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
)

public2的cmakelists

# set the project name
project (public2)
# add a library with the above sources
add_library(${
  project_name} public2.c)
add_library(lib::public2 alias ${
  project_name})
target_include_directories( ${
  project_name}
    public ${
  project_source_dir}
)

编译

	$  mkdir build
	$  cd build/
	$  cmake .. 
	$  make
	$  cd main
	./main
	main: cmake
	app1: cmake
	app1: end
	app2: cmake
	public2: cmake
	public2: end
	app2: end
	app3: cmake
	static hello: cmake
	app3: end
	public1: cmake
	public1: end
	public2: cmake
	public2: end
	static hello: cmake
	main: end
网站地图