El Capitan: Undefined symbol "start" whenever I compile a C program - c

I recently installed OS X El Capitan, and after I installed it I've been having a problem with my C linker. Whenever I try to compile any program, it says
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
This happens whether I use Clang installed from the Xcode developer tools, Clang installed from Homebrew, or GCC installed from Homebrew. It only started happening right after I installed El Capitan.

I've encountered similar issue.
After I upgraded my macpro to OS X El Capitan, I became unable to compile my program with GNU gcc installed from Macport. It shows same error message. However clang worked fine in my case.
I searched web and found this:
Undefined symbol "start" while linking D program through LD
It says add -lcrt1.o in gcc option.
I tried it and it did fix the problem, so you may try this one.
but I found more complete fix (below), so please try this one too.
In my case, problem was that I had two versions of ld: /usr/bin/ld and /opt/local/bin/ld. Somehow clang works fine with both versions of ld, but GNU gcc works well only with /usr/bin/ld.
So I changed PATH variable to tell GNU gcc to use /usr/bin/ld, then problem was fixed.

Related

Cross Compiling for x64 on ARM (Apple Silicon)

It is definitely possible to target Intel when compiling on an Apple Silicon (ARM64) system, as Xcode does that all the time when building universal bundles of an app. However, I am unable to replicate this compiling a C program with make (specifically Stockfish).
What I've tried
I'm invoking make like so: make build ARCH=x86-64-modern COMP=clang (the same command works when I substitute x86-64-modern for apple-silicon). I've tried using the gcc compiler, which also worked when targeting the apple-silicon arch.
The problem
The make build command terminates with a bunch of errors, most importantly:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Above the error is a bunch of lines complaining that a file was built for an "unknown bitcode architecture:
ld: warning: ignoring file <name>.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): <name>.o
So, it seems like the compilation phase succeeds, but the linking phase fails due to missing symbols. How would I acquire and provide the missing symbols to ld such that it can link successfully?

Compiling a C program that uses OpenGl 4 in Mac OS X

So I am learning OpenGL 4 from openglbook.
I copied and pasted the simple window creating program in my editor and followed Compiling OpenGL programs on OS X by using
gcc -o hello hello.c -framework OpenGL -framework GLUT
I got the following error:
Undefined symbols for architecture x86_64:
"_glewGetErrorString", referenced from:
_Initialize in chapter-c8ba2d.o
"_glewInit", referenced from:
_Initialize in chapter-c8ba2d.o
"_glutInitContextFlags", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextProfile", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextVersion", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutSetOption", referenced from:
_InitWindow in chapter-c8ba2d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I understand correctly, the problem is tha -framework GLUT means that you're linking against the GLUT framework shipped by Apple with macOS and located inside /System/Library/Frameworks folder. This GLUT implementation unfortunately lacks some functionality provided by current version of FreeGLUT. And OpenGLBook.com tutorials use some of this functionality, thus, as it is mentioned many times here, the code examples have to be compiled with FreeGLUT.
There are, of course, few ways this can be done. The easiest one is by installing freeglut by means of the infamous Homebrew package manager and then setting up the CMake project as described below.
So, first install freeglut
brew install freeglut
Then setup CMake project by adding the following lines into CMakeLists.txt
find_package(FreeGLUT CONFIG REQUIRED)
find_package(glew CONFIG REQUIRED)
link_libraries(FreeGLUT::freeglut GLEW::GLEW)

CBLAS mac OS X Undefined symbols for architecture x86_64 error

I'm trying different C linear algebra libraries for my projects and now I want to learn BLAS (CBLAS). I am trying to follow the tutorial here. I realised that cblas is already built in the xcode and by adding the flag
gcc foo.c -framework Accelerate
or
gcc foo.c -lcblas
I can remove most of the errors I had before. however there is a final error which I can not find anywhere on the internet.
Undefined symbols for architecture x86_64:
"_printVector", referenced from:
_main in blas1C-63e43d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm not sure where I'm making the mistake but there are a number of possibilities:
I need to instal BLAS/CBLAS from the netlib website. I actually tried to do this but I could manage to build the library from the source code! it would be great if somebody could make a Homebrew formula. then if the original CBLAS library installed then I need to learn about the gcc flags for compiling and how to link the libraries.
or there are syntax differences between the original CBLAS from netlib and the one built in the xcode and I need to change the code.
thanks for your help in advance.
P.S. I'm trying to compile the codes blas1C.c, blaio.c, blaio.h from the page I mentioned.
edit 1: oh my! I just realised that I have made a horrible mistake. the printVector function missing is not part of CBLAS but a function made by the author of the blog. the only thing I had to do was just to compile the blaio.c file as well. so the correct gcc command should be
gcc blas1C.c blaio.c -lcblas
or
gcc blac1C.c blaio.c -framework Accelerate
That function comes from the blasio.c in the website and declared in blasio.h
You need to build blasio and link to it

Issue compiling with f77 on OSX 10.11

I have been trying to run this model, written some decades ago by someone else. I've built the libraries, but when I try to actually run the program, I get the following error:
f77 -o mimics1.5 ../source/*.o trunk_hght_f.f
trunk_hght_f.f:
trunk_hght_func:
Undefined symbols for architecture x86_64:
"MAIN__", referenced from:
-u command line option
(maybe you meant: _MAIN__)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am on MacOSX 10.11, and which f77 gives /usr/local/bin/f77 and f77 -v gives back:
Cynthias-MacBook-Pro-2:input_dir cynthiag$ f77 -v
/usr/local/bin/f77 script based on f2c:
f2c (Fortran to C Translator) version 20100827.
Should f77 be in /usr/bin/instead?
ld seems to be the problem, so I followed instruction from HERE to try to solve it, but it looks like my XCode is up to date, that my libraries are built and linked (as far as I can tell), and I even reinstalled binutils to make sure ld was in teh right place, but no success.
which ld gives : /usr/bin/ld and ld -v gives:
Cynthias-MacBook-Pro-2:input_dir cynthiag$ ld -v
#(#)PROGRAM:ld PROJECT:ld64-264.3.102
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
LTO support using: LLVM version 7.3.0
I am new to fortran, so I probably messed up when setting up f77, but I've been stuck on this problem for a few days now, so any help would be greatly appreciated!
In the end, it seem like the solution for me was just to use gfortran instead of f77. This involved carefully switching all the mentions to f77 in the model (i.e. in all the Makefiles) and replacing them by gfortran.
A clean install of the libraries was then necessary, since apparently a library compiled in f77 will not be compatible with gfortran.
Finally, the model was calling on some executable without ./ which caused the model to failed.
Unfortunately, I can't give an explanation as to why f77 was failing, but gfortran was a lot easier to set up than f77/f2c and worked great, so it's probably the way to go.

libiconv issue when compiling SDL 2.0 on Mac OS X 10.6

I'm trying to compile SDL 2.0 on OS X 10.6, but I've been getting this message:
Undefined symbols:
"_libiconv_open", referenced from:
_SDL_iconv_string in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
"_libiconv", referenced from:
_SDL_iconv in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
"_libiconv_close", referenced from:
_SDL_iconv_string in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I've read the issues over at https://github.com/mxcl/homebrew/issues/894 and How to replace MacPort's libiconv with Mac's default 64-bit version? to see if either of them help. I uninstalled everything that MacPorts had since those two seemed to suggest that the issue was related to multiple versions of the same library (libiconv it had installed had way too many dependencies to manually uninstall them all, and I didn't need them anymore), but am still getting the same error. Any ideas?
So it turns out after uninstalling the MacPorts copy, I still had 2 copies of libiconv on my computer that were different. One was in /usr/lib, and the other was in /usr/local/lib. Compiling with the one in /usr/lib produced the error above, however adding -L/usr/local/lib to the EXTRA_LDFLAGS variable in the Makefile worked.
I found a simple solution. Just add 2 more parameters when configure SDL source:
./configure CPPFLAGS='-I/opt/local/include' LDFLAGS='-L/opt/local/lib'

Resources