User forums > Using Code::Blocks

Passing environment variables as programs' arguments

(1/2) > >>

qweweng:
Hello everyone, I have a quick question. I would like to pass an environment variable to my executable in C::B. Can this be done under Project -> Set programs' arguments?

For example, suppose I have an environment variable %VAR% = abc, and a program PrintArgument.exe that prints the first command line argument (argv[1]). In command prompt, I can type "PrintArgument %VAR%" and the output will be "abc". Can I achieve this with C::B?

I've tried to set the programs' argument to %VAR% under Project -> Set programs' arguments, but it does not work and the program output is %VAR%. I'm not sure if I'm doing this correctly.

I've searched the forum for this issue but couldn't find anything similar. I apologize if this is a double post.

I'm using Windows 7 and Code::Blocks 12.11 binary release by the way. Thanks in advance for your help.

ollydbg:

--- Quote from: qweweng on November 12, 2013, 03:45:23 pm ---...In command prompt, I can type "PrintArgument %VAR%" and the output will be "abc". ....

--- End quote ---
Does this works already when you run your program under command line?

If the answer is Yes, this means your program can do the job which replace %VAR% to abc, and print the abc.

If the answer is No, this means your program can NOT do the job, then why do you ask C::B to do the job? In this case, It is definitely a problem of your program, not C::B.

qweweng:

--- Quote from: ollydbg on November 12, 2013, 04:07:31 pm ---
--- Quote from: qweweng on November 12, 2013, 03:45:23 pm ---...In command prompt, I can type "PrintArgument %VAR%" and the output will be "abc". ....

--- End quote ---
Does this works already when you run your program under command line?


--- End quote ---
Yes. The code for the PrintArgument program is just as follows:

--- Code: ---int main(int argc, char* argv[])
{
    cout << argv[1] << endl;
    return 0;
}

--- End code ---
In other words, it's the command prompt that looks up the value of %VAR% (which is "abc") and passes the value into my program.


--- Quote from: ollydbg on November 12, 2013, 04:07:31 pm ---If the answer is Yes, this means your program can do the job which replace %VAR% to abc, and print the abc.

If the answer is No, this means your program can NOT do the job, then why do you ask C::B to do the job? In this case, It is definitely a problem of your program, not C::B.

--- End quote ---
PrintArgument is just my made-up program to help illustrate my question. I have a much larger program in which a value of an environment variable is required to be passed. Although I can compile the program in C::B and run it using command prompt, it would be more convenient if I can do both in C::B. Hence, I was wondering if C::B allows me to achieve this. I'll find other alternatives if this is not possible.

Thanks ollydbg for your speedy reply.  ;)

ollydbg:

--- Quote ---Although I can compile the program in C::B and run it using command prompt, it would be more convenient if I can do both in C::B. Hence, I was wondering if C::B allows me to achieve this. I'll find other alternatives if this is not possible.
--- End quote ---
Ok, I think C::B don't have this feature(expand the %VAR% string to its value in its argument settings)
You can enhance it by implement it, so patch are welcome.
Another way I think you can use is to use the Tool+ plugin, which you can use the command line tool, you can see a related discuss here:
http://developer.berlios.de/bugs/?func=detailbug&bug_id=19180&group_id=5358

So, write some command like (I don't try this yet, you can try it yourself)


--- Code: ---cmd /c $(TARGET_NAME) %VAR%

--- End code ---

See: http://wiki.codeblocks.org/index.php?title=Variable_expansion as another reference.

EDIT:

--- Quote ---Variables which are neither global user variables nor builtin types are replaced with a value provided in the project file, or with an environment variable if the latter should fail.
--- End quote ---
So, maybe, C::B already replace the %XXX% like string reference to Variable expansion - CodeBlocks, so just enable the macro replace feature for the argument input dialog?


Jenna:
At least on linux, the environment variables get expanded:

--- Code: ---#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    for (int i=0; i < argc; ++i)
        cout << argv[i] << endl;
    cout << "Hello world!" << endl;
    return 0;
}

--- End code ---
called with argument $HOME (set in "Project -> Set programs's arguments..." gives the (expected) result:

--- Quote ---/tmp/test/bin/Debug/test
/home/jens
Hello world!

Process returned 0 (0x0)   execution time : 0.002 s
Press ENTER to continue.

--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version