wzpan / cmake-demo

《CMake入门实战》源码

Home Page:http://www.hahack.com/codes/cmake/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

为什么demo4中, USE_MYMATH默认是ON,编译出来pow使用的标准库的函数

LyleLee opened this issue · comments

[me@centos Demo4]$ cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/tmp/cmake-demo/Demo4
[me@centos Demo4]$ make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[me@centos Demo4]$ ./Demo 3 4
Now we use the standard library.
3 ^ 4 is 81
[me@centos Demo4]$

事实上,加了message("${USE_MYMATH}")能看到是ON, 但是最后的链接结果使用的标准库。只有用ccmake跑一次,设置为ON或者OFF, 才会真正链接或者不链接own libarary。 直接cmake不知道为什么没有得到预期结果。

update一下:

第一次 cmake . 链接的是标准库
第二次cmake,链接的是own库
以后cmake,都链接是own库

第一次ccmake . 链接取决是USE_MYMATH是否ON
第二次ccmake. 效果一样

试试:

image

谢谢, 我知道可以按c设置on和off。 ccmake 之后, 按c设置之后,输出结果总是正确的。现在的问题是:cmake 执行两次的结果不一样:

[me@centos Demo4]$ ls
CMakeLists.txt  config.h.in  main.cc  math
[me@centos Demo4]$ cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/tmp/cmake-demo/Demo4
[me@centos Demo4]$ make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[me@centos Demo4]$ ./Demo 3 4
Now we use the standard library.
3 ^ 4 is 81
[me@centos Demo4]$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/tmp/cmake-demo/Demo4
[me@centos Demo4]$ make
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[me@centos Demo4]$ ./Demo 3 4
Now we use our own Math library.
3 ^ 4 is 81
[me@centos Demo4]$

请您试试。

那个 option 只是用来声明有一个配置项:

option (USE_MYMATH
	   "Use provided math implementation" ON)

这里的 ON 不是默认值,而是出现在 ccmake 里的时候应该展示的初始值。如果你没有按 c 再按 g,这个值是不会生效的。

详见:https://stackoverflow.com/a/44720601

不需要调出图形界面也可以使用这个ON的值, 提交了一个PR. 可以解决这个执行两次的疑问。

ok,谢谢!