使用命令行参数设置#define标识符 [英] Setting #define identifier using command line argument

查看:504
本文介绍了使用命令行参数设置#define标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个python脚本来运行一个编译过的二进制文件,其源代码是用C ++编写的,使用命令行参数改变一个参数,作为控制。



os.system(./ my_program+ my_arg)



让我们说 main()



而我想在一些包含的头文件中使用
#define MY_PARAM my_val
其中 my_val 作为程序运行时的命令行参数。 / p>

我正在使用gcc编译



任何想法?

解决方案

愚蠢的问题。我想我没有正确理解我试图解决之前问的问题!我通过以下方法解决了这个问题:


  1. 使用-D选项在makefile中添加可选参数来设置这些编译时变量。这将用指定的变量名替换为运行 make 时给出的值。


  2. 一个python脚本循环通过一系列选项,每次调用 os.system(make OPT1 =+ str )+OPT2 =+ str(opt2))等,后跟os.system(./ myprog | tee mylogfile.log)


  3. 然后,python脚本解析日志文件,该文件包含的输出作为 make 的参数输入的选项的函数而变化。

    li>

希望这可以帮助任何想做类似事情的人。


I'm writing a python script to run a compiled binary whose source was written in C++, varying a parameter as controlled using a command line argument.

e.g. os.system("./my_program " + my_arg)

Let's say it's too much hassle changing this parameter from main().

Instead I'd like to set it in some included header file using #define MY_PARAM my_val where my_val is taken as a command line argument when the program is run.

I'm using gcc to compile

Any ideas?

解决方案

Dumb question. I guess I didn't properly understand the problem I was trying to solve before asking! I got past this problem by:

  1. Adding optional arguments in the makefile using the -D option to set these compile-time variables. This replaces specified variable names with the values given when running make.

  2. A python script looped through a bunch of options, each time calling os.system("make OPT1=" + str(opt1) + "OPT2=" + str(opt2)) etc. followed by os.system("./myprog | tee mylogfile.log")

  3. The python script then parsed the logfile, which contained output that varied as a function of the options inputted as arguments to make.

Hope this helps anyone wanting to do something similar.

这篇关于使用命令行参数设置#define标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆