首页 > 其他分享 >Tool-CMake-OPTION

Tool-CMake-OPTION

时间:2023-04-29 13:46:13浏览次数:40  
标签:CMake OPTION Tool component SRCS config USB

Tool-CMake-OPTION

https://clubjuggler.livejournal.com/138364.html

includes a component

As an example, consider a project that optionally includes a component that communiates via USB and allows the user, at compile time, to specify whether to include that component or not.
The CMakeLists.txt file to do that looks like this:

PROJECT(myproject)
OPTION(WITH_USB "Include our USB component" OFF)
SET(SRCS file1.c file2.c file3.c)
IF(WITH_USB)
  SET(SRCS ${SRCS} usbfile1.c usbfile2.c)
ENDIF(WITH_USB)
ADD_EXECUTABLE(myproject ${SRCS})

The OPTION(...) line creates a parameter WITH_USB and set its default value to OFF. CMake also includes a help string to describe what the option does. Based on the value of WITH_USB, the variable SRCS either includes or excludes the two USB-related files usbfile1.c and usbfile2.c. SRCS is then passed to the ADD_EXECUTABLE call to define the program’s source files.
To include USB support, simply enable WITH_USB like this:
$ cmake –DWITH_USB=ON /path/to/source/files


the optional USB support a library in a subdirectory

PROJECT(myproject)
OPTION(WITH_USB "Include our USB component" OFF)
SET(SRCS file1.c file2.c file3.c)
ADD_EXECUTABLE(myproject ${SRCS})
IF(WITH_USB)
  ADD_DIRECTORY(usblib)
  TARGET_LINK_LIBRARIES(myproject usblib)
ENDIF(WITH_USB)

Given this file, the USB source files would be placed in a subdirectory called usblib along with a new CMakeLists.txt file:

PROJECT(usblib)
SET(SRCS usbfile1.c usbfile2.c)
ADD_LIBRARY(usblib ${SRCS})

Now, if USB support is enabled, CMake builds the USB library and link sit into the executable.

But how does the code code know to use the optional USB code?

First, the USB-specific code should be surrounded with a #ifdef WITH_USB.
Next, CMake processes a special configuration file, substituting placeholders with CMake variables.

Let’s make a new file called config.h.cmake that contains this:

#ifndef TEST_CONFIG_H
#define TEST_CONFIG_H

#cmakedefine WITH_USB

#endif

Then, in the CMakeLists.txt file, modify the first part to look like this:

PROJECT(myproject)
OPTION(WITH_USB "Include our USB component" OFF)
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.cmake 
  ${CMAKE_BINARY_DIR}/config.h)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})

Given this, CMake translates config.h.cmake to create config.h in the build directory. The INCLUDE_DIRECTORIES command tells the compiler to add both the source directory and the build directory to the include path. If WITH_USB is enabled, CMake replaces #cmakedefine WITH_USB with #define WITH_USB. Otherwise, it replaces #cmakedefine WITH_USB with /*#undef WITH_USB*/. As long as you #include config.h and surround the USB code with #ifdef WITH_USB, everything just works. The only downside at the moment is that config.h.cmake must be created manually.

标签:CMake,OPTION,Tool,component,SRCS,config,USB
From: https://www.cnblogs.com/yongchao/p/17363915.html

相关文章

  • Tool-CMake-How CMake simplifies the build process by Bruno Abinader
    Tool-CMake-HowCMakesimplifiesthebuildprocessbyBrunoAbinaderhttps://gitlab.kitware.com/cmake/community/-/wikis/homehttps://brunoabinader.github.io/2009/12/07/how-cmake-simplifies-the-build-process-part-1-basic-build-system/https://brunoabin......
  • ubuntu安装VMware tools
    以下是在Ubuntu上安装VMwareTools的步骤:代码操作打开VMware虚拟机并进入虚拟机控制台。单击菜单栏的“VM”菜单,选择“InstallVMwareTools”。在弹出窗口中选择“DownloadandInstall”(下载并安装)。在Ubuntu中使用管理员权限打开终端。在终端中输入以下命令,以自动将VMwareTools......
  • Tool-CMake-A Simple CMake Example
    Tool-CMake-ASimpleCMakeExamplehttps://cmake.org/examples/Therearethreedirectoriesinvolved.Thetopleveldirectoryhastwosubdirectoriescalled./Demoand./Hello.Inthedirectory./Hello,alibraryisbuilt.Inthedirectory./Demo,anexecuta......
  • TypeError: this.libOptions.parse is not a function
    安装完node.js运行项目后,报错:TypeError:this.libOptions.parseisnotafunctionatESLint8Plugin.<anonymous>(C:\ProgramFiles\JetBrains\GoLand2022.1.4\plugins\JavaScriptLanguage\languageService\eslint\bin\eslint8-plugin.js:139:64)atstep......
  • CMakeLists---自定义变量-add_definitions()函数
    转载:https://blog.csdn.net/qq_35699473/article/details/115837708引言其实这个函数在安装一些库的时候,它的CMakeLists里面就有这样的函数。典型的就是opencv了。opencv安装时候有一些指令也是针对这个函数的,比如安装命令(随便搜索的):cmake ../opencv-3.4.1-DWITH_GTK_2......
  • CLion远程调试CMake项目
    cmake项目的远程调试。CLion安装在本地Windows系统,cmake项目部署在远程的Linux系统。配置远程调试可以比较方便地进行Linux端的cmake项目开发。点击setting。主要配置这三个:Toolchains点击+号,添加RemoteHost。然后先点击Credentials的设置按钮,添加远程连接。配置SSHCon......
  • The principle of uploading files with command line tools All In One
    TheprincipleofuploadingfileswithcommandlinetoolsAllInOne命令行工具文件上传的原理/TheprincipleofcommandlinetoolfileuploaddemospipgitCDNOSS{"name":"xui","version":"1.0.0","main&q......
  • Chrome devTools--节点监听
    节点监听dombreakpoints:子节点修改/属性修改/节点移除子节点修改适用场景:当鼠标移入下拉框时,下拉选项出现,想要选中下拉选项dom,修改下拉选项的dom时,却又消失了解决方案:选中body节点,监听dom字节点的修改,Breakon---》subtreemodifications,下拉选项触发时进入debugger 节......
  • 使用ethtool排查网卡速率问题
    今天去现场帮一个客户排查备份网络速率问题。用户期望是万兆的速率,但实际上目前只有千兆,因为目前上面运行着数据库,且数据量较大,千兆的备份网络速率不能满足用户备份数据库的时长要求。首先,确认备份网络是由两块网卡(eth3,eth4)做了bonding,起名为bondeth1。使用ethtool查看底层的et......
  • hutool 日期转换
    DateTime转LocalDateDateTimeuseTimeDate=DateUtil.offsetMonth(date,12);LocalDateTimeuseTime=DateUtil.toLocalDateTime(useTimeDate);Java8日期时间API,新增了LocalDate、LocalDateTime、LocalTime等线程安全类:LocalDate:只有日期,诸如:2019-07-13LocalTime:只有时间,诸......