I'd like to add ~/include to my include path for all projects while using Xcode, something like setting the environment variable CPLUS_INCLUDE_PATH in Linux. (See here for the related Linux question.)
Is this possible in Xcode? I tried setting the above environment variable, but it doesn't seem to work. (And if it is possible, I'd like to also set related paths, like LIBRARY_PATH and LD_LIBRARY_PATH.)
According to Apple's Docs, you are able to provide a default for any build setting using an environment variable.
In this case, you'd want to set HEADER_SEARCH_PATHS. For some reason I don't understand, this doesn't work. It works fine for other build settings (for example, OTHER_CFLAGS), but not for HEADER_SEARCH_PATHS. You can see what the variable name for any setting is by opening the research assistant in the build settings window (book button on the bottom left.)
One var that does work is USER_HEADER_SEARCH_PATHS, which is just like what you want, but only works for paths in double quotes (not in angle brackets.)
So
#include "bar.h"
would work, but
#include <bar.h>
wouldn't.
The build settings plist referenced from the above article on environment variables should end up looking something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>USER_HEADER_SEARCH_PATHS</key>
<string>~/test</string>
</dict>
</plist>
Hope that helps.
For any of you blessed darlings that are trying to achieve this, or any other hack of ~/.MacOSX/environment.plist, aka global environmental variables in this post-Mountain-Lion world, check out EnvPane - An OS X preference pane for environment variables
EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. This works even for changes made by manually editing ~/.MacOSX/environment.plist, not just changes made via the preference pane.
Add the HEADER_SEARCH_PATHS build setting to your Xcode project. This setting takes a space separated list of paths and if the path ends in a double star (**), it will search subdirectories recursively. So to answer for your example, set HEADER_SEARCH_PATHS to:
~/include
If you had a number of paths to include, you would set it to something like:
~/include ~/my_other_includes/** /usr/local/special_frameworks/**
Just select a .c or .m file in your "Groups & Files Tab" and choose "get Info" through RMB,
then under "build" you can add additional Compiler Flags like -I for additional include searchpath.
Build settings are not environment variables, and environment variables are not build settings. Setting an environment variable will not affect Xcode builds.
USER_HEADER_SEARCH_PATHS is a build setting, and putting a list of paths into it will achieve what you want. If you set the value in a target's inspector, it will take effect for only that target (and only for the Build Configurations you designate). If you set it in the Project inspector, it will take effect in all targets in the project, unless a target overrides it with its own setting.
For paths that are specific to your machine, you should probably define a Source Tree in the Xcode Preferences, such as LOCAL_INCLUDE = ~/include . Then define USER_HEADER_SEARCH_PATHS = $(LOCAL_INCLUDE) in the project. This way, other people can open your project and build it by setting their Source Tree to the particular location of the local includes on their machine, without having to change the project file.
Related
MacOS is continually using Xcode to open various files in my OS. I already know how to set a default app opener for all files with a particular extension (.txt, .py, etc...), but I can't use this feature with "extensionless" files like .bash_profile. Is there a workaround for this other than changing the default app for each file?
My preferred solution would be if I could tell MacOS to never use Xcode as a default app opener. I'm assuming there is some config file buried away that might help me achieve this but I haven't been able to find anything to help me with this.
Looking for the same I found this answer stop-xcode-from-hijacking-my-file-associations on apple.stackexchange.
It seems that XCode is pretty persistent but using this 3rd party app https://github.com/Lord-Kamina/SwiftDefaultApps you can achieve it!
Before;
After;
Instructions;
Step 1: After installing the app, go to System preferences > SwiftDefaultApps
Step 2: Go to the Uniform Type Identifiers tab and look for public data.
Step 3: Assign, under the Editor section, the desired editor.
Step 4: Apply (the app is a little bit clumsy and sometimes it doesn't show the confirmation popup, if this is the case, just close the preference window, open it again and check that everything is ok!)
Since asking this question, I stumbled upon this MacOS system file:
~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
I've found that you can add your own dict entries into the LSHandlers array. For instance, I added the following entry to bind all public.data file types to my Sublime application:
<dict>
<key>LSHandlerContentType</key>
<string>public.data</string>
<key>LSHandlerPreferredVersions</key>
<dict>
<key>LSHandlerRoleAll</key>
<string>-</string>
</dict>
<key>LSHandlerRoleAll</key>
<string>com.sublimetext.4</string>
</dict>
Right click on a file where you want to change the default editor
Choose Get Info (Or simply select the file and press CMD+I)
Select the editor in the Open with section
Click Change All..
Now all files of this type will be opened with the editor you selected.
I've got a folder reference in Xcode that needs to be configurable by the user. I'd prefer to use a script or plist to set its Location and Full Path properties at build time. I've looked around the web but can't seem to find a way of doing this - is there?
I have Ruby installed, but I still need to add it to the PATH variable. I found something online for how to manually add it using the command line:
set PATH=C:\Ruby200-x64\bin;%PATH%
But before I try it, I want to be sure it's not going to overwrite what's currently in the PATH variable. (I have no experience with this stuff so I don't know what to expect).
Thanks in advance for your help!
first, notice that this question is not really about Ruby, rather about how to set a path in windows (it work the same way if you want to add an executable different from Ruby)
second, you are not overwriting the PATH environment variable because you add the existing content of the same to the new one you are setting in:
set PATH=C:\Ruby200-x64\bin;%PATH%
the %PATH% is the current content of the PATH variable.
Consider using
set PATH=%PATH%;C:\Ruby200-x64\bin
instead, this will make your OS search the original path before searching the ruby bin folder. Maybe it makes few difference on modern computers, but my old DOS days claim the second solution is better.
third and last point, in Windows you can set environment variables in control panel / system properties
How to get there depends on the version of your OS, but if you search for the ambient variables and system variables you should get there.
From the Desktop, right-click the very bottom left corner of the screen to get the Task Menu.
From the Task Menu, click System.
Click the Advanced System Settings link in the left column.
In the System Properties window, click on the Advanced tab, then click the Environment Variables button near the bottom of that tab.
In the Environment Variables window (pictured below), highlight the Path variable in the "System variables" section and click the Edit button.
Add or modify the path lines with the paths you want the computer to access. For ruby it will be:
;YOUR_RUBY_INSTALLATION_PATH\bin;
The operation with set PATH=C:\Ruby200-x64\bin;%PATH% is probably only temporary until you restart your computer.
I just wanted to let everyone know that when you install rubyinstaller on Windows and follow its steps, there is no option to 'add to path variables' because it automatically adds it.
Rubyinstaller trolled me hard because it said gem not found when I did gem install sass immediately after install.
Your path variable is probably already set if you used rubyinstaller.
The trick is to open CMD or I would imagine, PowerShell, ConEMU, etc, git bash, and type gem.
Press WINKEY and type cmd
Type gem install sass (or whatever else that is in the bin folder for Ruby)
I just went to add the PATH variable, and it was already set, so my problem was the garbage command line tool that opened after installing rubyinstaller.
You can also do these steps to add to the PATH variables:
Press WINKEY
Type view advanced system settings
Open that
Click Environment Variables
Click Path in the list
Click Edit
Check if C:\Ruby24-x64\bin is already there, if so, done
Click New and type in C:\Ruby24-x64\bin
Done
Yes, this is correct. In your example %PATH% will be expanded to the current value of the PATH variable, so this command is effectively adding a new entry to the beginning of the PATH.
Note that calling set PATH will only affect the current shell. If you want to make this change permanent for all shells, the simplest option is to set it as a user variable using the Environment Variables dialog box.
On Windows 8 you can open this dialog by hitting Win+s and searching for 'environment variables'. On earlier versions of Windows you can right-click 'My Computer', choose Properties, then Advanced system settings, then Environment variables. You can create (or update) a PATH variable in the user variables section and add whatever entries you need. These will be appended to the existing system path. If you take this approach you will need to open a new cmd shell after updating the variables.
Fear nothing, What you are doing is prepend C:\Ruby200-x64\bin to the existing %PATH%, this is what the command you posted does.
The path is a list of directories, separated by ;, in which the system will look for the command you execute.
In your case it's:
C:\Ruby200-x64\bin
%PATH%, if you print it on the command line, you'll find it's itself a list of directories separated by ;.
In case you want to make your change permanent, you have to change your PATH sytemwide.
For CLI, as noted elsewhere calling SET on the path variable only acts on the current window and closing it or restarting windows voids the change.
Example the transient Version selected as the answer:
set "PATH=%PATH%;C:\Ruby200-x64\bin"
To correctly set the path permanently in CLI use the path command:
PATH %PATH%;C:\Ruby200-x64\bin
This will persist between CMD windows and after reboots.
I know this is questions has the Windows tag, however it is one of the first DuckDuckGo results for "ruby gems add to path" so I just wanted to add this.
On Linux you can add this line to the end of your .bashrc in order to add it to your path:
export PATH=$PATH:~/.gem/ruby/2.6.0/bin
I have the following situation:
I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment variable, let's name it ENV_VAR. I want to set ENV_VAR to some value for my application only because if I set it for the whole system it may breaks some other apps. And it should work transparently to the user i.e. he just run my app from the Application folder by double clicking it. How can I achieve it?
NOTE: dynamic libraries are loaded before main functions starts hence setting this variable in the main doesn't help.
You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).
<key>LSEnvironment</key>
<dict>
<key>ENV_VAR</key>
<string>value</string>
</dict>
However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed
This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.
I am not sure if the following works because I don't have such an app to try. The idea is to set the environment variable from the terminal, then call your application:
ENV_VAR=something open -a YourApplication
I am new to both Cocoa programming and Xcode. I am wondering how I can setenv (or set environment variable) using MainMenu.nib (or .xib) file. I have someone else's code in the main function like this:
[NSApplication sharedApplication];
[NSBundle loadNibNamed:#"MainMenu" owner: NSApp];
After the second line, it can get an environment variable:
if (!getenv("R_HOME")) {
fprintf(stderr, "R_HOME is not set.\n");
return -1;
}
I want to know how one can construct a nib(or xib) file, e.g., MainMenu.xib, in Xcode 3's latest version so that it can be used to setenv. One thing that I did was to code setenv without using a nib file:
setenv("R_HOME", "/Library/Frameworks/R.framework/Resources", 1)
But, this did not work when I execute the Cocoa GUI programming by double clicking the xxx.app although that worked when I execute the program's command line version. So, it seems like that I need a way to set environment variables when a Mac GUI application is launched. I have seen other ways of setting environment variables, but I want to know how one can set environment variables using nib (or xib) files, and by loading it using NSBundle's loadNibNamed method.
I want to know how one can construct a nib(or xib) file, e.g.,
MainMenu.xib, in Xcode 3's latest version so that it can be used to
setenv.
Nib files don't have anything to do with environment variables. They don't contain any code, and they have no impact on the program other than supplying data used to instantiate classes provided by the application's code. I suppose you could write a class that sets environment variables and then use a nib to instantiate that class, but there'd be no difference between doing that and just instantiating the class in your code.
One thing that I did was to code setenv without using a nib
file:...But, this did not work when I execute the Cocoa GUI
programming by double clicking the xxx.app although that worked when I
execute the program's command line version.
How do you know it didn't work? Are you calling getenv() to check the value of R_HOME in your code, or are you using a command like env in the terminal? If the latter, you're not looking at the same environment where the variable was set.
So, it seems like that I need a way to set environment variables when
a Mac GUI application is launched.
What are you trying to accomplish? It seems unlikely that you'd be setting an environment variable for your own program's use -- it already knows that value, so going out the environment seems pointless. Are you trying to communicate with another program?
I have seen other ways of setting environment variables, but I want to
know how one can set environment variables using nib (or xib) files,
and by loading it using NSBundle's loadNibNamed method.
Again, there's really no intersection between nib files and environment variables. If you want to be able to modify your app's behavior based on some external variable, you can set environment variables in your .login file, and those values should be readable by your app. Perhaps a better solution would be to use the defaults system -- your app can read and write values in the defaults system via NSUserDefaults, and you can read and write those same values at the command line using the defaults command.
Environment variables are usually set along with options in your schemes.
From the "Product" menu choose "Edit scheme" and select your desired target. Now on the right hand side choose the "Arguments" tab.
On the "Arguments" tab you can set command line parameters and environment variables to be passed along to the running application.
To set environment variables in your distributable application, such that they will be set even for users (Xcode only affects your debugging runs), set the LSEnvironment property in your app's Info.plist.