在make类构建系统配置文件中定义函数宏

CMakeLists中定义函数宏

add_definitions(-D'sysctlbyname(name,oldp,oldlenp,ewp,newlen) = -1')

这个写法会报warning:

CMake Warning (dev) at /ssd1t/helloworld/CMakeLists.txt:114:
  Syntax Warning in cmake code at column 64

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

原因是‘=’号前面需要有空格。

写成下面两种都可以:

add_definitions(-D'sysctlbyname(name,oldp,oldlenp,ewp,newlen) =-1')
add_definitions(-D'sysctlbyname(name,oldp,oldlenp,ewp,newlen) = -1')

虽然可以通过,但是在实际编译的时候会报错,原因是cmake并不支持function-style的macro定义:

Preprocessor definitions for compiling a target’s sources.

The COMPILE_DEFINITIONS property may be set to a semicolon-separated list of preprocessor definitions using the syntax VAR or VAR=value. Function-style definitions are not supported. CMake will automatically escape the value correctly for the native build system (note that CMake language syntax may require escapes to specify some values).

Makefile中定义函数宏

直接通过g++命令测试,编译一个helloworld.cpp的文件,报错:

$ g++ -Dsysctlbyname\(name, oldp, oldlenp, newp, newlen\)=1 helloworld.cpp 

<command-line>: error: expected parameter name, found "1"
helloworld.cpp: In function ‘int main(int, char**)’:
helloworld.cpp:28:7: error: ‘sysctlbyname’ was not declared in this scope
   28 |   if (sysctlbyname (0, 1, 2, 3, 4) == -1)
      |       ^~~~~~~~~~~~

修改为下面这样正常,这个宏用单引号包起来:

g++ -D'sysctlbyname(name, oldp, oldlenp, newp, newlen)=1' helloworld.cpp

这里使用了单引号 ' 来包围整个宏定义,可以确保如果宏定义中包含空格或特殊字符,它们会被正确地视为宏定义的一部分。

放到Makefile里面,即:

ifndef HAVE_SYSCTLBYNAME
CXXFLAGS += -D'sysctlbyname(name, oldp, oldlenp, newp, newlen)=-1'
endif

相关推荐

  1. make构建系统配置文件定义函数

    2024-07-09 21:40:09       23 阅读
  2. rust嵌入式之用函数简写状态机定义

    2024-07-09 21:40:09       46 阅读
  3. 构建完成,通知我:Gradle配置构建通知

    2024-07-09 21:40:09       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:40:09       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:40:09       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:40:09       57 阅读
  4. Python语言-面向对象

    2024-07-09 21:40:09       68 阅读

热门阅读

  1. lvs集群

    lvs集群

    2024-07-09 21:40:09      21 阅读
  2. 记录Linux安装go环境的一个坑

    2024-07-09 21:40:09       22 阅读
  3. Python开发——Python 字典的使用

    2024-07-09 21:40:09       22 阅读
  4. 流媒体技术

    2024-07-09 21:40:09       24 阅读
  5. mysql面试题 Day5

    2024-07-09 21:40:09       22 阅读
  6. Xcode简介

    2024-07-09 21:40:09       22 阅读