Author Topic: Passing command line options to compiler  (Read 11258 times)

Offline JasperC

  • Single posting newcomer
  • *
  • Posts: 6
Passing command line options to compiler
« on: October 19, 2011, 11:40:27 am »
Hello all,

I'm a new to using Code::Blocks, so far I've been using gedit and the command line to compile my programs.  I'd like to move to CB, but I've run into a problem.  I'm running CB 10.05 on Ubuntu 11.04 x32, and my code makes use of ROOT.  To compile, I've been using the following at the Terminal:

Code
g++ mycode.c `root-config --cflags --libs`

I'm currently stuck trying to pass that last bit to the compiler when using CB.  I've read this, and under

Project -> Build options... -> [Project name] -> [GNU GCC Compiler] -> Compiler settings -> Other options, and
Project -> Build options... -> [Project name] -> [GNU GCC Compiler] -> Linker settings -> Other linker options

I have added in

Code
`root-config --cflags --libs`

However when I build the project, the build log shows that that arguement is not passed to g++.


Project -> Build options... -> [Project name] -> [GNU GCC Compiler] -> Compiler settings -> Other options
does have a "-fexceptions" already added, and that does get passed, so I'm not sure what could be wrong.  Advice would be appreciated, thanks!

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Passing command line options to compiler
« Reply #1 on: October 19, 2011, 12:02:20 pm »
The argument between the backticks will be expanded and the result is put into the commandline.
If you turn on full commandline logging ( http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_an_compiler_problem.3F ) , you should be able to see it.

Offline JasperC

  • Single posting newcomer
  • *
  • Posts: 6
Re: Passing command line options to compiler
« Reply #2 on: October 20, 2011, 02:44:26 am »
Hi Jens,

Thanks for the reply.  Guess I wasn't that clear in my post, but I have read the FAQ and already turned on full commandline logging.  The problem is the backticks(?) argument does not appear in the commandline, either expanded or unexpanded.

There is already a "-fexceptions" option specified, and I can remove and restore that with the expected result.  My argument seems to be ignored however...

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Passing command line options to compiler
« Reply #3 on: October 20, 2011, 04:54:37 am »
When you run the following on a command line, what is the result?
Code
root-config --cflags --libs
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline JasperC

  • Single posting newcomer
  • *
  • Posts: 6
Re: Passing command line options to compiler
« Reply #4 on: October 20, 2011, 07:28:20 am »
When you run the following on a command line, what is the result?
Code
root-config --cflags --libs

I get
Code
-pthread -m32 -I/home/jasper/gate/root_5.28.00e/include -L/home/jasper/gate/root_5.28.00e/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic

So I know that part of it should work at least...

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Passing command line options to compiler
« Reply #5 on: October 20, 2011, 09:55:16 am »
If you start C::B from a console with the -d parameter:
Code
codeblocks -d
you should have an additional tab in "Logs & others" called "Code::Blocks Debug".

After opening your project you can onspect it and see, whether there is an entry like:
Code
Caching result of `root-config --cflags --libs`
Cached

You can also try to run the command from the pre- or pos-build steps of the program (without the backticks), to see the output of it, if it is run in the context of C::B.
If it has an error, C::B will cache nothing and expand the backticked command to ... nothing.

Offline JasperC

  • Single posting newcomer
  • *
  • Posts: 6
Re: Passing command line options to compiler
« Reply #6 on: October 20, 2011, 11:14:30 am »
Thanks again.  I think I know where I've gone wrong now... :oops:  Though I might still need help to fix it.

If you start C::B from a console with the -d parameter:
Code
codeblocks -d
you should have an additional tab in "Logs & others" called "Code::Blocks Debug".

After opening your project you can onspect it and see, whether there is an entry like:
Code
Caching result of `root-config --cflags --libs`
Cached

You can also try to run the command from the pre- or pos-build steps of the program (without the backticks), to see the output of it, if it is run in the context of C::B.
If it has an error, C::B will cache nothing and expand the backticked command to ... nothing.

So I did that and the build process worked fine...  Then I started a new Terminal window and tried again - this time it didn't work.  Then I remembered that for my first Terminal window, the one where I normally work from, I manually run a script to set up the environment:

Code
. /home/jasper/gate/root_5.28.00e/bin/thisroot.sh

I would have thought that that sets the variables for everything else, but it appears to only affect the current Terminal window (and apparently applications launched from it).  And normally I start C::B from the launcher...

This line appears under the "Code::Blocks Debug" tab in both cases:
Code
Caching result of `root-config --cflags --libs`
Cached
but with nothing else that I can see that indicates success or failure. (Am I looking in the wrong place?)

When successful, `root-config --cflags --libs` expands normally.


New question then, is it possible to get C::B to run the script before calling g++?  I'm making the assumption that C::B is internally creating it's own "session" to do the compile and link.  I've tried adding the above script command to

Project -> Build options... -> [Project name] -> [GNU GCC Compiler] -> Pre/post build steps -> Pre-build steps

but the build fails, and the build log shows:

Code
Running project pre-build steps
. /home/jasper/gate/root_5.28.00e/bin/thisroot.sh
/home/jasper/gate/root_5.28.00e/bin/thisroot.sh: 25: Bad substitution
Process terminated with status 2 (0 minutes, 0 seconds)
0 errors, 0 warnings


Removing the leading ". " I get:

Code
Running project pre-build steps
/home/jasper/gate/root_5.28.00e/bin/thisroot.sh
/bin/sh: /home/jasper/gate/root_5.28.00e/bin/thisroot.sh: Permission denied
Process terminated with status 126 (0 minutes, 0 seconds)
0 errors, 0 warnings

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Passing command line options to compiler
« Reply #7 on: October 20, 2011, 01:34:23 pm »
If you only need to run root-config, you can run it with full absolute pathname:
Code
`/path/to/root-config --cflags`
in the "Compiler options -> Other options" and
Code
`/path/to/root-config --libs`
in the "Linker settings -> Other linker options".
It might be useful to put it in the global compiler settings, if you have created a special compiler for root.

About the issue running the shell-script:
I don't know why it fails, looks like missing execution rights for the user who runs C::B, or a script-error.
But this leaves the scope of our forum, sorry.

Offline JasperC

  • Single posting newcomer
  • *
  • Posts: 6
Re: Passing command line options to compiler
« Reply #8 on: October 21, 2011, 02:54:49 am »
Hi Jens, not a problem.  Thanks for all you help.  :D  (And the solution you suggested does seem to work.)