CMAKE TIPs

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.

你可能感兴趣的:(cmake,CMAKE)