gcc -D 选项和setenv

本文介绍了GCC编译器中的宏定义方法及其使用场景,通过实例演示了如何使用-D选项来定义宏并进行条件编译和宏替换。此外,还详细解释了setenv函数的作用及使用方法,包括如何在程序中更改或增加环境变量。
一、概念 

gcc的-D选项可以定义宏,这是什么意思呢?

C语言源程序中有#define定义的宏,可以起到替换、条件编译的功能;定义宏的方式是放在头文件或者C文件中。gcc提供了另外一种宏定义的方法,当然可以想象这样的宏定义方法并不是很好用,但毕竟编译器给我们提供了额外的宏定义的方法。假设程序需要很多宏,不可能这些宏都在编译器中定义,可以说比较重要的宏才会放在gcc的D选项后边。

-Dname 定义宏name,默认定义内容为字符串“1” 
-Dname=defn 定义宏name,并且内容为defn

二、实验

#include <stdio.h> int main()
{
    #ifdef HELLO
    printf("HELLO defined !\n");
    printf("HELLO = %d\n",HELLO); #else printf("HELLO not define!\n"); #endif return 0;
}

1.执行编译命令:

#gcc main.c -o main
main的执行结果 :
HELLO not define!

2.执行编译命令:(条件编译测试)
#gcc -DHELLO main.c -o main
main的执行结果 :
HELLO defined!
HELLO = 1

3.执行编译命令:(宏替换测试)
#gcc -DHELLO=36 main.c -o main
main的执行结果 :
HELLO defined!
HELLO = 36


setenv

首先要说明的是,通过此函数并不能添加或修改 shell 进程的环境变量,或者说通过setenv函数设置的环境变量只在本进程,而且是本次执行中有效。如果在某一次运行程序时执行了setenv函数,进程终止后再次运行该程序,上次的设置是无效的,上次设置的环境变量是不能读到的。

表头文件 #include<stdlib.h>
定义函数 int setenv(const char *name,const char * value,int overwrite);
函数说明 setenv()用来改变或增加环境变量的内容。参数name为环境变量名称字符串。参数 value则为变量内容,参数overwrite用来决定是否要改变已存在的环境变量。如果没有此环境变量则无论overwrite为何值均添加此环境变量。若环境变量存在,当overwrite不为0时,原内容会被改为参数value所指的变量内容;当overwrite为0时,则参数value会被忽略。返回值 执行成功则返回0,有错误发生时返回-1。

> remotes::install_version("matrixStats", version = "1.1.0") Downloading package from url: https://cran.rstudio.com//src/contrib/Archive/matrixStats/matrixStats_1.1.0.tar.gz 将程序包安装入‘/home/lenovo/R/x86_64-pc-linux-gnu-library/4.5’ (因为‘lib’没有被指定) * installing *source* package ‘matrixStats’ ... ** this is package ‘matrixStats’ version ‘1.1.0’ ** 成功将‘matrixStats’程序包解包并MD5检查 ** using staged installation ** libs using C compiler: ‘gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0’ gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c 000.init.c -o 000.init.o gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c allocMatrix2.c -o allocMatrix2.o gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c anyMissing.c -o anyMissing.o gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c binCounts.c -o binCounts.o binCounts.c: In function ‘binCounts’: binCounts.c:25:81: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘R_xlen_t’ {aka ‘long int’} [-Wformat=] 25 | error("Argument 'bx' must specify at least two bin boundaries (= one bin): %d", xlength(bx)); | ~^ | | | int | %ld gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c binMeans.c -o binMeans.o binMeans.c: In function ‘binMeans’: binMeans.c:26:60: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘R_xlen_t’ {aka ‘long int’} [-Wformat=] 26 | error("Argument 'y' and 'x' are of different lengths: %d != %d", ny, nx); | ~^ ~~ | | | | int R_xlen_t {aka long int} | %ld binMeans.c:26:66: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘R_xlen_t’ {aka ‘long int’} [-Wformat=] 26 | error("Argument 'y' and 'x' are of different lengths: %d != %d", ny, nx); | ~^ ~~ | | | | int R_xlen_t {aka long int} | %ld binMeans.c:33:81: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘R_xlen_t’ {aka ‘long int’} [-Wformat=] 33 | error("Argument 'bx' must specify at least two bin boundaries (= one bin): %d", xlength(bx)); | ~^ | | | int | %ld gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c colCounts.c -o colCounts.o gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c colOrderStats.c -o colOrderStats.o colOrderStats.c: In function ‘colOrderStats’: colOrderStats.c:58:47: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘R_xlen_t’ {aka ‘long int’} [-Wformat=] 58 | error("Argument 'which' is out of range: %d", qq + 1); | ~^ ~~~~~~ | | | | int R_xlen_t {aka long int} | %ld gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c colRanges.c -o colRanges.o gcc -I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c diff2.c -o diff2.o In file included from diff2_lowlevel.h:15, from diff2.c:10: diff2_lowlevel_template.h: In function ‘diff2_int’: diff2_lowlevel_template.h:75:11: warning: implicit declaration of function ‘Calloc’; did you mean ‘calloc’? [-Wimplicit-function-declaration] 75 | tmp = Calloc(nidxs - lag, X_C_TYPE); | ^~~~~~ | calloc In file included from diff2_lowlevel_template.h:22, from diff2_lowlevel.h:15, from diff2.c:10: 000.templates-types.h:40:22: error: expected expression before ‘int’ 40 | #define X_C_TYPE int | ^~~ diff2_lowlevel_template.h:75:31: note: in expansion of macro ‘X_C_TYPE’ 75 | tmp = Calloc(nidxs - lag, X_C_TYPE); | ^~~~~~~~ In file included from diff2_lowlevel.h:15, from diff2.c:10: diff2_lowlevel_template.h:119:5: warning: implicit declaration of function ‘Free’; did you mean ‘free’? [-Wimplicit-function-declaration] 119 | Free(tmp); | ^~~~ | free In file included from diff2_lowlevel_template.h:22, from diff2_lowlevel.h:19, from diff2.c:10: diff2_lowlevel_template.h: In function ‘diff2_dbl’: 000.templates-types.h:50:22: error: expected expression before ‘double’ 50 | #define X_C_TYPE double | ^~~~~~ diff2_lowlevel_template.h:75:31: note: in expansion of macro ‘X_C_TYPE’ 75 | tmp = Calloc(nidxs - lag, X_C_TYPE); | ^~~~~~~~ make: *** [/usr/local/lib/R/etc/Makeconf:202:diff2.o] 错误 1 ERROR: compilation failed for package ‘matrixStats’ * removing ‘/home/lenovo/R/x86_64-pc-linux-gnu-library/4.5/matrixStats’ * restoring previous ‘/home/lenovo/R/x86_64-pc-linux-gnu-library/4.5/matrixStats’
最新发布
11-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值