Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc on Mac OS X: how to link libraries installed with MacPorts?

I have installed gcc 4.6 using macports. The prefix is /opt/local, and I get the expected include path:

#include "..." search starts here:
#include <...> search starts here:  
/opt/local/include/gcc46/c++/  
/opt/local/include/gcc46/c++//x86_64-apple-darwin10  
/opt/local/include/gcc46/c++//backward  
/opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1/include  
/opt/local/include  
/opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1/include-fixed  
/usr/include  
/System/Library/Frameworks  
/Library/Frameworks End of search list.

However, /opt/local/lib doesn't seem to be in the library search path, so I have to specify it with -L/opt/local/lib when using g++ on command line:

Library search paths:
    /opt/local/lib/gcc46/gcc/x86_64-apple-darwin10/4.6.1
    /opt/local/lib/gcc46
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/

This is a problem for other libraries installed with macports. Is there an easy way to add /opt/local/lib to the library search path? I have tried setting DYLD_LIBRARY_PATH to no avail. I am using Mac OS X 10.6.8.

like image 764
juanchopanza Avatar asked Jul 18 '11 18:07

juanchopanza


People also ask

How do I know if MacPorts is installed on my Mac?

Get the MacPorts base here: http://www.macports.org. Click on the DOWNLOAD link and grab the . dmg disk image for whichever OS you may have. Confirm that MacPorts is installed by typing "which port" and verifying that it returns /opt/local/bin/port.

Is GCC pre installed in Mac?

The gcc application will be installed by default in /usr/local/bin. Personally, I use Apple's clang/clang++ compilation tools rather than deal with GNU gcc. If you have the most recent Apple Command Line Tools (macOS 10.

How do you use ports on a Mac?

MacPorts uses autoconf and makefiles for installation. These commands will build and install MacPorts to /opt/local . You can add --prefix to ./configure to relocate MacPorts to another directory if needed. Now MacPorts will look for portfiles in the working copy and use Git instead of rsync to update your ports tree.


2 Answers

I found the easiest way is to set C_INCLUDE_PATH and LIBRARY_PATH:

export C_INCLUDE_PATH=/opt/local/include
export CPLUS_INCLUDE_PATH=/opt/local/include
export LIBRARY_PATH=/opt/local/lib
like image 123
nwellnhof Avatar answered Oct 16 '22 18:10

nwellnhof


It depends if you want to link your executable dynamic or static against a library. Under OS X you add the libraries as source/object files like this:

 Dynamic: g++ -Wall -o myexecutable myfile.cpp /path/to/library.dylib
 Static: g++ -Wall -o myexecutable myfile.cpp /path/to/library.a

The best way is to use a build system, for example CMake (which can be installed from macports). And makes it very easy to find libraries, create libraries in a crossplatform way.

like image 20
Jerry Jacobs Avatar answered Oct 16 '22 17:10

Jerry Jacobs