CMAKE TIPs

本文介绍了如何在CMake中跳过默认编译器检查,通过设置`CMAKE_C_COMPILER_WORKS`等变量来避免编译器测试。同时,针对Windows环境,强调了CMAKE需要nmake.exe,以及未安装Visual Studio可能导致的问题。此外,还讲解了在多层级目录结构下,如何正确使用`add_subdirectory()`包含子目录的CMakeLists.txt文件。
摘要由CSDN通过智能技术生成

1. How to avoid CMAKE check default compiler

CMAKE will check default compiler when start CMAKE.

If you have CC,CXX in environment variable, CMAKE will use CC,CXX to compile a simple code.

But some compiler can't pass this test.

So we need find a way to jump this test.

Here is a way to jump compiler check.

cmake_minimum_required (VERSION 3.10)

# jump compiler check.
set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_ASM_COMPILER_WORKS TRUE)

# project info
project(MY_PROJECT)

Attention:

Looks like 'set(CMAKE_C_COMPILER_WORKS TRUE)' need before 'project(MY_PROJECT)'.

2. CMAKE requires nmake.exe on Windows

On Windows, CMAKE will need nmake.exe.

nmake.exe is in virtual studio.

And CMAKE's default compiler on windows is virtual studio compiler.

If you don't install virtual studio, you may get error of missing nmake.exe.

If you copy nmake.exe to your windows, you may get error of missing compiler.

If you want to jump the compiler check, TIP 1 is for you.

Attention:

After cmake created Makefile, you need to run nmake to build it. Not make!!!!

3. Include same level folder's CMakeLists.txt

If we have folders like:

Top_level_folder
     |-----Projects
     |       |----CMakeList.txt
     |
     |-----liba_folder
     |       |----CMakeList.txt
     |       |----liba1.c
     |       |----liba2.c
     |
     |-----app_folder
             |----CMakeList.txt
             |----main.c  

So the project/CMakeList.txt need to call app_folder/CMakeList.txt and liba_folder/CMakeList.txt

In project/CMakeList.txt we need use:

add_subdirectory(../liba_folder ./output/liba)
add_subdirectory(../app_folder ./output/app)

'./output/liba' and './output/app' is necessary for add_subdirectory() function.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值