Mac OS X ld: library not found for -lpython3 - macos

I'm trying to follow the steps to compile some C code to import into Python from the example given here: http://csl.sublevel3.org/C-functions-from-Python/
I can compile it using the suggested invocation:
gcc -dynamiclib -I/usr/include/python2.3/ -lpython2.3 -o myModule.dylib myModule.c
But I'm using Python 3, I've muddled through and resolved a bunch of compiler errors, to end up with this:
gcc -dynamiclib -I/Library/Frameworks/Python.framework/Versions/3.2/include/python3.2m/ -lpython3.2m -o myModule.dylib myModule.c
But now I'm stuck, this gives the following error:
ld: library not found for -lpython3.2m
From my limited knowledge I tried some things out. I've found that I have two locations where Python versions exist:
/System/Library/Frameworks/Python.framework/Versions
and also:
/Library/Frameworks/Python.framework/Versions
Python 3.2 is located in the second place, so I think that somehow I need to tell gcc where to search to resolve the -lpython3.2m? Or that -lpython3.2m is wrong, I've tried -lpython3.2 but I get the same type of error from ld.
Other facts that might help:
Mac OS 10.6.8
Python 3.2 installed from the DMG that you get from Python.org
If there is any other information that I can give you to help you to help me then please let me know

To tell gcc additional directories to search for libraries, you use the -L option, similar to the -I option you're already using. Something like -L/Library/Frameworks/Python.framework/Versions/3.2/lib, I would guess.

Related

What is the meaning of 'ld: library not found for -lm'

I'm a beginner at Fortran code. I install gfortran from the homebrew to my apple silicon mac. The path show in the terminal when I type which fortran is /opt/homebrew/bin/gfortran
I try to use bash shell to run a 'hello world' fortran file on the Desktop (.f90 file). I typed the following command in the terminal. gfortran prog01_01.f90. But it does not work. The error says: ld: library not found for -lm and collect2: error: ld returned 1 exit status. I have no idea how to deal with this problem.
Can anyone please help me this out?
This means that the compiler cannot find the basic mathematical library libm. It should normally be included in your GLIBC or other standard C library installation and be available in the appropriate library directories and I would expect that your package manager would complain that something is missing.
Anyway, you simply have to fix your software installation, we cannot tell you more without details about your system and your software packages. You can try to install GLIBC if you did not install it yet.

ld: building for macOS-x86_64 but attempting to link with file built for macOS-x86_64

I have this strange issue where creating / using a static library works in my Ubuntu VM but not on macOS:
ld: warning: ignoring file ./dist/libXXXX.a, building for macOS-x86_64 but attempting to link with file built for macOS-x86_64
Command to create the static library is:
ar rcs libtest.a obj1.o obj2.o ...
Compiler invocation:
gcc -g -Wall -Wextra main.c -L./dist -lXXXX -o main
Searching on google didn't yield any usable results except for this (maybe) related question on SO:
Possible related question
I realize this is an old post and you found your fix, but let me post this here for anyone else who runs into this problem for whom these answers don't provide a solution.
You might be using two different toolchains unknowingly, one from Apple (installed via Xcode) and one from GNU (installed via Home-brew or MacPorts). If you type ranlib --version and see version info showing that ranlib is GNU, this is likely the case.
Make sure that /usr/bin comes in your $PATH before /usr/local/bin and /opt/local/bin. When you run which -a ranlib, the first result in the list should be /usr/bin/ranlib. Same for which -a ar-- the first result should be /usr/bin/ar. If it is not so, you need to fix your $PATH.
Once you fix your path and clean your project, try building again and things should work.
The issue was solved when I directly put those object files rather than gathering them into a static library, i.e.,
gcc -g -Wall -Wextra main.c obj1.o obj2.o -o main
After that, I got many warnings like ld: warning: object file (obj1.o) was built for newer macOS version (11.0) than being linked (10.14), but it is a warning, and the object is linked, so the problem is solved.
The root cause is that some library passes -mmacosx-version-min=10.14 to gcc, so the object file is built for 10.14, but my macos is now 11.0.
If you want to make things work, try directly using object files rather than creating a static library.
If you want to resolve all the warnings, find ``-mmacosx-version-min` and comment it.
After looking at my script that automatically creates the static library I've found the culprit:
For some reason my tool created object files for header files (resulting in files like header.h.o).
Removing those fixed the issue.

How to compile fortran code to run without gfortran installed

I have downloaded Bellhop, which is an underwater acoustic simulator written in Fortran. It can be found here with the Makefile.
Question 1: I would like to know if it is possible to compile Fortran code, including everything needed, so a user without gfortran installed, can run it.
I have read here the following:
static linking
This section does not apply to Windows users, except for Cygwin users with gcc4-4.3.2-2 or later.
gfortran is composed of two main parts: the compiler, which creates the executable program from your code, and the library, which is used when you run your program afterwards. That explains why, if gfortran is installed in a non-standard directory, it may compile your code fine but the executable may fail with an error message like library not found. One way to avoid this (more ideas can be found on the binaries page) is to use the so-called "static linking", available with option -static gfortran then put the library code inside the program created, thus enabling it to run without the library present (like, on a computer where gfortran is not installed). Complete example is:
gfortran -static myfile.f -o program.exe
Reading this, I suppose that it is possible to do what I'm asking but I'm not very familiarized with fortran and makefiles. I don't understand this:
put the library code inside the program created
Question 2: How can I put the library code inside the program? Where can I find the library? What does "inside the program" means?
I'm running OSX 10.9.4 and gfortran
I solved my problem about compiling Fortran code with gfortran using static libraries.
As #M.S.B. said, using static-libgfortran worked for me under MacOS.
If somebody is having issues with linking the libquadmath.0.dylb library, remove libquadmath.0.dylib and libquadmath.dylib from /usr/local/gfortran/lib/
This doest the trick. Further information can be found here
I think the meaning of the bold part is actually
gfortran then puts the library code inside the
program created
That means using -static should be enough, there is no additional step. Just be advised you will need a static version of all the libraries that you link with.
I know this is very old tracker, but maybe somebody will be still interested in the solution that works.
Let's say we have code:
! fort_sample.f90
program main
write (*,*) 'Hello'
stop
end
First, compile the stuff:
gfortran -c -o fort_sample.o fort_sample.f90
Then, link stuff
ld -o ./fort_sample -no_compact_unwind \
-arch x86_64 -macosx_version_min 10.12.0 \
-lSystem \
/usr/local/gfortran/lib/libgfortran.a \
/usr/local/gfortran/lib/libquadmath.a \
/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0/libgcc.a \
fort_sample.o
You can execute it
./fort_sample
Hello
You can notice that quadmath is no longer there
> otool -L fort_sample
fort_sample:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.51.1)
I guess this is what you were looking for in a first place. No removing dylibs, no symbolic links, etc.
The current version of the option is -static-libgfortran. This means that the Fortran specific libraries of gfortran will be included into the executable. These are libraries are automatically found for a good installation of gfortran. This should produce an executable that should run on other computers with the same OS, even if that computer doesn't have gfortran installed. This option likely doesn't statically link all libraries, so there is some risk that some other shared library used on your computer won't be available on the other computer.

Linker Error PPC

I'm trying to compile a c++11 code under a IBook G4.
I'm using a g++-4.9 installed with brew
However, I get stuck with this error:
/usr/bin/ld: can't locate file for: -lcrt0.o
Found nothing in the net about crt. Maybe, someone has better experience dealing with dinosaurs..
Frustration of having a compiler able to run on multiple architectures with the flip of a command line switch. Use -arch and specify either native or target cpu.

GTK+ 3 won't setup/configure in Windows

Just recently I tried to install GTK+ on Windows 7 using all-in-one bundle and latest MinGW package. I proceeded with all steps of official tutorial. Lurking through several tutorials, especially this one helped me getting MinGW and MSYS set up, so GCC sure works. The command
pkg-config --cflags --libs gtk+-3.0
printed out the expected set of paths/options. However, when I tried to run either
gcc -o gtk3.exe gtk3.c (pasted 'pkg-config' output)
or
gcc -o gtk3.exe gtk3.c `pkg-config --cflags --libs gtk+-3.0`
I've got
gcc: error: gtk3.c: No such file or directory
as a result. Searching the file manually was no success as well. It seems that Windows command prompt has torn the original long command apart, but I don't think that it would be this way if GCC has initialized gtk3.c.
I believe that official tutorial can't be wrong, so please help me resolve this problem.
Best regards, Mathias.
A Windows installation tutorial for GTK+ might not be the most thoroughly tested thing... This is a better version of that page, it includes a link to the source file (although any GTK+ "hello world" code will do).
You could file a bug on the missing link and images as well if you want to help others.

Resources