繁体   English   中英

如何导出变量(在Makefile,shell脚本中,…),以便将其视为C语言中的定义(作为宏)?

[英]How to export variable [in Makefile, shell script, …] to be seen as defined in C (as macro)?

我写了用两个简单的功能md4opensslmd4crytpo++ 现在,我想使代码更具“可移植性”,并能够根据安装了哪个库( opensslcrypto++ )用户使用该功能。 我知道如何检查是否已安装库:

dpkg --status $openssl | grep -q not-installed

if [ $? -eq 0 ]; then
    echo "OpenSSL is not installed."
fi

但是,如何导出此类信息以在C代码中看到呢? 就像是:

dpkg --status $openssl | grep -q not-installed

if [ $? -eq 0 ]; then
    export has_openssl=false
fi

然后,在C代码中:

#if !defined has_openssl
use function from cryptopp
#else
use function from openssl
#endif

那可能吗? 如果是这样,我该怎么做? 在我之前的问题中, 如何检查是否已安装openssl或cryptopp并使用系统(已安装)中实际存在的库? ,用户@MattMcNabb提出了解决方案,但我对他提到的Makefile部分不太熟悉。

我假设您必须从Shell脚本中调用编译器。

您可以使用以下内容进行简单定义:

gcc -Dhas_openssl

或未定义:

gcc -Uhas_openssl

对于更复杂的定义,可以使用例如:

gcc -Dname=\"John\"

这意味着您可以按以下方式调用编译器(当然,您必须在gcc编译器行中提及源文件名):

dpkg --status $openssl | grep -q not-installed

if [ $? -eq 0 ]; then
    gcc -Uhas_openssl
else
    gcc -Dhas_openssl
fi

然后,您可以使用第一个定义为

#if !defined has_openssl
  use function from cryptopp
#else
  use function from openssl
#endif

为Ya牛编辑:

您可以从makefile中执行与从脚本中完全相同的操作:

gcc -Dhas_openssl

要么

gcc -Uhas_openssl

您可以使用带有“ stub”参数的所有gcc语句制作一个初始makefile。 例如:

gcc ***param1*** source.c
gcc ***param1*** anotherSource.c
gcc ***param1*** yetAnOtherSource.c

然后,在启动makefile之前,从脚本中进行更改

***param1***

在-Dhas_openssl或-Uhas_openssl中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM