/** * power - Calculate the power of number. * @param base: Base value. * @param exponent: Exponent value. * * @return base raised to the power exponent. */ doublepower(double base, int exponent){ double result = base; int i;
if (exponent == 0) return1;
for(i = 1; i < exponent; ++i) result = result * base;
return result; }
intmain(int argc, char *argv[]){ if (argc < 3){ printf("Usage: %s base exponent \n", argv[0]); return1; } double base = atof(argv[1]); int exponent = atoi(argv[2]); double result = power(base, exponent); printf("%g ^ %d is %g\n", base, exponent, result); return0; }
intmain(int argc, char *argv[]) { if (argc < 3){ printf("Usage: %s base exponent \n", argv[0]); return1; } double base = atof(argv[1]); int exponent = atoi(argv[2]);
#ifdef USE_MYMATH printf("Now we use our own Math library. \n"); double result = power(base, exponent); #else printf("Now we use the standard library. \n"); double result = pow(base, exponent); #endif printf("%g ^ %d is %g\n", base, exponent, result); return0; }
$ cmake .. -DUSE_MYMATH=ON -- Building for: Visual Studio 16 2019 ... -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (11.7s) -- Generating done (0.1s) -- Build files have been written to: D:/src/04/build
$ cmake --build . 用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642 版权所有(C) Microsoft Corporation。保留所有权利。
1>Checking Build System Building Custom Rule D:/src/04/math/CMakeLists.txt MathFunctions.cc MathFunctions.vcxproj -> D:\src\04\build\math\Debug\MathFunctions.lib Building Custom Rule D:/src/04/CMakeLists.txt main.cc Demo.vcxproj -> D:\src\04\build\Debug\Demo.exe 'pwsh.exe' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 Building Custom Rule D:/src/04/CMakeLists.txt
$ ./Debug/Demo.exe 2, 10 Now we use our own Math library. 2 ^ 10 is 1024
此时 config.h 的内容为:
1
#define USE_MYMATH
USE_MYMATH 为 OFF 运行结果:
1 2 3 4 5
[ehome@xman Demo4]$ ./Demo Now we use the standard library. 7 ^ 3 = 343.000000 10 ^ 5 = 100000.000000 2 ^ 10 = 1024.000000
此时 config.h 的内容为:
1
/* #undef USE_MYMATH */
安装和测试
CMake 也可以指定安装规则,以及添加测试。这两个功能分别可以通过在产生 Makefile 后使用 make install 和 make test 来执行。在以前的 GNU Makefile 里,你可能需要为此编写 install 和 test 两个伪目标和相应的规则,但在 CMake 里,这样的工作同样只需要简单的调用几条命令。
[ehome@xman Demo5]$ make test Running tests... Test project /home/ehome/Documents/programming/C/power/Demo5 Start 1: test_run 1/4 Test Start 2: test_5_2 2/4 Test Start 3: test_10_5 3/4 Test Start 4: test_2_10 4/4 Test
# 检查系统是否支持 pow 函数 include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake) check_function_exists (pow HAVE_POW)
将上面这段代码放在 configure_file 命令前。
接下来修改 config.h.in 文件,预定义相关的宏变量。
1
#cmakedefine HAVE_POW
最后一步是修改 main.cc ,在代码中使用宏和函数:
1 2 3 4 5 6 7
#ifdef HAVE_POW printf("Now we use the standard library. \n"); double result = pow(base, exponent); #else printf("Now we use our own Math library. \n"); double result = power(base, exponent); #endif
printf("%s Version %d.%d\n", argv[0], Demo_VERSION_MAJOR, Demo_VERSION_MINOR); printf("Usage: %s base exponent \n", argv[0]); return1; } double base = atof(argv[1]); int exponent = atoi(argv[2]);
#if defined (HAVE_POW) printf("Now we use the standard library. \n"); double result = pow(base, exponent); #else printf("Now we use our own Math library. \n"); double result = power(base, exponent); #endif
printf("%g ^ %d is %g\n", base, exponent, result); return0; }
# 构建一个 CPack 安装包 include (InstallRequiredSystemLibraries) set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}") set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}") include (CPack)
[ehome@xman Demo8]$ sh Demo8-1.0.1-Linux.sh Demo8 Installer Version: 1.0.1, Copyright (c) Humanity This is a self-extracting archive. The archive will be extracted to: /home/ehome/Documents/programming/C/power/Demo8
If you want to stop extracting, please press <ctrl-C>. The MIT License (MIT)
Copyright (c) 2013 Joseph Pan(http://hahack.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Do you accept the license? [yN]: y By default the Demo8 will be installed in: "/home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux" Do you want to include the subdirectory Demo8-1.0.1-Linux? Saying no will install in: "/home/ehome/Documents/programming/C/power/Demo8" [Yn]: y
Using target directory: /home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux Extracting, please wait...
Unpacking finished successfully
完成后提示安装到了 Demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:
1 2 3
[ehome@xman Demo8]$ ./Demo8-1.0.1-Linux/bin/Demo 5 2 Now we use our own Math library. 5 ^ 2 is 25