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

cmake 简单的例子-ag真人游戏

阅读 : 219

目录结构

└── 01-hello-cmake
├── cmakelists.txt
└── main.c

源文件

main.c

#include 
int main(int argc, char *argv[])
{
  
   printf("main: cmake\r\n");
   return 0;
}

cmakelists.txt

# set the minimum version of cmake that can be used
# to find the cmake version run
# $ cmake --version
cmake_minimum_required(version 3.5)
# set the project name
project(hello_cmake)
# add an executable
add_executable(hello_cmake main.c)

第一行用于指定cmake最低版本
第二行指定项目名称(这个名称是任意的)
第三行指定编译一个可执行文件,hello_cmake 是第一个参数,表示生成可执行文件的文件名(这个文件名也是任意的),第二个参数main.c则用于指定源文件。

编译

	$  mkdir build
	$  cd build/
	$  cmake ..
	$  make

测试

	$  ./hello_cmake
	main:cmake

目录结构

├── cmakelists.txt
├── include
│ └── hello.h
└── src
├── hello.c
└── main.c

源文件

main.c

#include "hello.h"
int main(int argc, char *argv[])
{
  
    hello_print();
    return 0;
}

hello.c

#include "hello.h"
void hello_print(void)
{
  
    printf("hello: cmake\r\n");
}

头文件

hello.h

#ifndef __hello_h__
#define __hello_h__
#include 
void hello_print(void);
#endif

cmakelists.txt

# set the minimum version of cmake that can be used
# to find the cmake version run
# $ cmake --version
cmake_minimum_required(version 3.5)
# set the project name
project(hello_headers)
# create a sources variable with a link to all cpp files to compile
set(sources
    src/hello.c
    src/main.c
)
# 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
)

第三行创建一个sources变量,其中包含一个指向要编译的所有c文件的链接。
第五行添加头文件目录。

注意

include_directories()和target_include_directories()的区别。

编译

	$  mkdir build
	$  cd build/
	$  cmake ..
	$  make

测试

	$  ./hello_cmake
	hello: cmake
网站地图