Passing environment variables automatically into XCode

Hi all,

I'm trying to develop a command line tool in OS X that I had from Linux.
Everything goes well into compiling but I can't run it inside Xcode cause it can't find some libraries.

This app relies heavily in library paths and variable sets that I have already in my bashrc to be set automatically whenever I open a terminal window (they are too many to just put by hand one by one in XCode, and I also have to run an .sh program to set some more variables).

So I would like to know if there is some way of just open Xcode from a terminal window with those environment variables and have them passed to Xcode?

I should add that the app compiles perfectly (it also needs some environment variable set for that so, at least for the compilation part XCode seems to be getting my var sets and library paths from the command line), it's when I try to run it automatically inside Xcode that it complains about missing libraries. I also can run it correctly if I do it from the command line.

One last thing to add, is that in NetBeans this works, i.e. if I open NetBeans IDE from the command line then it "knows" all the environment variables I had set. In Eclipse it doesn't work.

But I want to develop this in XCode since Netbeans crashes a lot in OS X.

MacBook Intel, Mac OS X (10.5.5)

Posted on Nov 18, 2008 9:24 AM

Reply
12 replies

Nov 19, 2008 9:03 AM in response to jb.morgado

jb.morgado wrote:


When I start Xcode from the terminal window, it indeed gets those variables for compiling the source, but when I do a compile and run, the run doesn't get those environment variables and so it doesn't run.


Which is what the first solution I posted would address.

These are a part of the environment variables set every time I open my terminal window, they are a lot and it would be quite boring and a waste of time to set them by hand inside XCode ... specially cause some of them will change every time I update those libraries:


If you think your project is a boring waste of time then I'm not sure what to tell you.

Nov 19, 2008 4:15 AM in response to orangekay

Hi again,

thank you for the help, but I don't think you are understanding what I'm asking for.
I'm using these C++ libraries that define lots of environment variables, please see bellow.

When I start Xcode from the terminal window, it indeed gets those variables for compiling the source, but when I do a compile and run, the run doesn't get those environment variables and so it doesn't run.

These are a part of the environment variables set every time I open my terminal window, they are a lot and it would be quite boring and a waste of time to set them by hand inside XCode ... specially cause some of them will change every time I update those libraries:
On this machine the G4SYSTEM=Darwin-g++
On this machine the G4INSTALL=/Applications/geant4.9.1.p02
On this machine the G4LIB=/Applications/geant4.9.1.p02/lib
On this machine the G4LEVELGAMMADATA=/Applications/geant4.9.1.p02/data/PhotonEvaporation2.0
On this machine the G4RADIOACTIVEDATA=/Applications/geant4.9.1.p02/data/RadioactiveDecay3.2
On this machine the G4LEDATA=/Applications/geant4.9.1.p02/data/G4EMLOW5.1
On this machine the G4NEUTRONHPDATA=/Applications/geant4.9.1.p02/data/G4NDL3.12
On this machine the G4ABLADATA=/Applications/geant4.9.1.p02/data/G4ABLA3.0
On this machine the CLHEP BASEDIR=/Applications/CLHEP
On this machine the CLHEP INCLUDEDIR=/Applications/CLHEP/include
On this machine the CLHEP LIBDIR=/Applications/CLHEP/lib
On this machine the CLHEP_LIB=CLHEP
On this machine the G4ANALYSIS_USE=1
On this machine the G4VIS BUILD_DAWNDRIVER=1
On this machine the G4VIS BUILD_OPENGLXDRIVER=1
On this machine the G4VIS BUILD_RAYTRACERXDRIVER=1
On this machine the G4VIS USEDAWN=1
On this machine the G4VIS USEOPENGLX=1
On this machine the G4VIS USERAYTRACERX=1
On this machine the XMFLAGS=
On this machine the XMLIBS=
On this machine the XMFLAGS=
On this machine the XAWFLAGS=
On this machine the XAWLIBS=
On this machine the G4LIB BUILDZLIB=1
On this machine the G4LIB USEZLIB=1
On this machine the G4LIB BUILDSHARED=1
On this machine the G4LIB USEGRANULAR=1
On this machine the G4UI USETCSH=1

LD LIBRARYPATH is set to include CLHEP and Geant4 libraries.

Nov 19, 2008 9:27 AM in response to jb.morgado

This sounds like some old-school corporate UNIX software. As such, you really aren't able to run the executable directly. You have to create a wrapper script that sets the proper environment variables and then calls the executable. You may be able to hack up your Xcode project to call this wrapper script to execute and debug your application. Then again, you may not. You may have to test by executing from the command line. If you have to debug, execute the debug version (via your wrapper script) and then attach the debugger to it.

Nov 20, 2008 8:32 AM in response to orangekay

{quote:title=orangekay wrote:}
Which is what the first solution I posted would address.

{quote}
No it does not, cause that way I have to put every environment variable by hand and it's very clear that is not what I asked for.

{quote:title=orangekay wrote:}
If you think your project is a boring waste of time then I'm not sure what to tell you.

{quote}
I may not be a native English speaker but I believe I express myself good enough. I clearly said that it was a waste of time the need to put those environment variables in XCode by hand instead of it automatically get them from the terminal window in which I open it. I never said my project was either boring or a waste of time ... at most I said XCode is boring or a waste of time.

Nov 20, 2008 10:14 AM in response to jb.morgado

jb.morgado wrote:
Ok, that is my main point or deception here, the fact that I cannot use the debugger this way since I cannot run the code from XCode, but only from the command line 😟


You can attach to any running process from the debugger. If said process has debugging information, it will be almost as if you had started it from the debugger. In a worst-case scenario, when you need to debug the initialization code, you might have to add a 20 second delay at startup in debug mode to give you a chance to attach the debugger before the code you want to look at starts executing.

I don't understand exactly what you are saying here. You mean I can run something like my .bash_profile file from within XCode and then my executable?


Actually, a better idea would be to take all those environment variables out of .bash_profile and put them into another script that you will use as a wrapper for your application. You can just source the wrapper from .bash_profile. From Xcode, you should be able to call the wrapper and run/debug your application. You will probably have to hack around on the executable settings to make that happen. Again, you can always start your application outside of Xcode and then attach to it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Passing environment variables automatically into XCode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.