How to fix " Undefined symbols for architecture x86_64 " in C? - c

I have a header file included in the main but when I compile the main, I have an error saying that the linker failed.
I tried to find the object files but I cannot find them.
I think the problem may come from my machine. I am kind of a beginner so I don't know how to solve this
When I try compiling my code I get this error:
Undefined symbols for architecture x86_64:
"_intClassic", referenced from:
_main in main-53b7e4.o
"_intQuadrature", referenced from:
_main in main-53b7e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#zwol #JonathanLeffer I have 3 files in my project main.c, integral.h
and integral.c. integral.c contains the code of the functions
intClassic and intQuadrature that allow me to calculate different
types of integral. In integral.h I declared the functions and
structures I use. Finally in the main I included integral.h .
Also $ gcc -o output file1.o file2.o can this command help me ?

In the same directory as your files, try running the command
gcc main.c integral.c -o integral
This should take the 2 files and compile them into a program called ./integral

Related

Xcode C project - linker command failed

The problem is as follows.
I am trying to add two simple .obj files to my project. I have added a header file and I can see that obj is valid as Ttime structure, which is a part of obj is working. However, whenever I try to execute any function I get this error:
Undefined symbols for architecture x86_64:
"_printRES", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error Screenshot

Can gcc -o and -S be used together

Can I use for example
gcc -o -S output.s abs.c
to generate an assembly file with name output.s? It seems like I can't. When I try to do that, I got following error message.
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)
I do not intend to use the linker, just try to examine the assembly code.
-o must be followed by the name of the output file. So, this would work:
gcc -S abc.c -o output.s

Trouble Linking when compiling a MEX file

I am trying to compile a mex file that I wrote in order to run a pre-existing C program in MATLAB. I am getting the following errors:
>> mex src/main.c -Iinclude -I/Users/my_name/mpfr-3.1.1/src/ -I/Users/my_name/gmp-5.0.5 -Lsrc0 -output cpdetect_c
Undefined symbols for architecture x86_64:
"_binomial_main", referenced from:
_cpdetect in main.o
"_gaussian1_main", referenced from:
_cpdetect in main.o
"_gaussianU_main", referenced from:
_cpdetect in main.o
"_mpfr_clears", referenced from:
_cpdetect in main.o
"_mpfr_exp10", referenced from:
_cpdetect in main.o
"_mpfr_inits2", referenced from:
_cpdetect in main.o
"_mpfr_set_d", referenced from:
_cpdetect in main.o
"_poisson_main", referenced from:
_cpdetect in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am confused because these functions are all part of files that I #Include in my code. How do I get the compiler to find them?
I am running MATLAB R2013a on Mac OS X 10.8 with Xcode version 4.6.3.
Thanks for any advice!
UPDATE1:
#Shai: Contents of my src0 folder:
>> ls src0
Makefile gaussian1.o test_gaussian1.c test_tools.c
Makefile.am loadtraj.c test_gaussian1_point_calc.c test_tools.o
Makefile.in loadtraj.o test_load tools.c
binomial.c main.c test_load.c tools.o
binomial.o main.o test_load.o
cpdetect poisson.c test_poisson.c
gaussian1.c poisson.o test_tools
This C program (originally meant to run on the command line) came from a friend and already had the src0 folder present. I modified the code files in the 'src' folder when writing the mex function, but I didn't touch the src0 folder.
UPDATE 2:
I realized I was pointing to the wrong location for mpfr. I am now using the following compile command:
>> mex src/main.c src/binomial.c src/gaussian1.c src/gaussianU.c src/poisson.c -Iinclude -I/usr/local/include -lmpfr -output cpdetect_c
and it is giving the following errors:
Undefined symbols for architecture x86_64:
"_mpfr_mul_d", referenced from:
_gaussian1_calc_constant_part in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
"_mpfr_printf", referenced from:
_find_gaussian1_change_point in gaussian1.o
_gaussian1_point_calc in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
_find_gaussianU_change_point in gaussianU.o
_find_poisson_change_point in poisson.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
mex: link of ' "cpdetect_c.mexmaci64"' failed.
This is strange. It's not like the compiler can't find any of the mpfr functions - there are lots of others that it seems to be finding without any trouble (and if I deliberately omit the -lmpfr flag, I get a much longer list of undefined symbols). I did confirm that mpfr_mul_d and mpfr_printf are part of the mpfr distribution I installed. Any ideas? Thanks.
The answer was provided by Amro - it is necessary to both point to the location of the library (using the flag -L/usr/local/lib in this case) and to tell the compiler to link to it (using the flag -lmpfr). Thanks also to Shai for help troubleshooting this.

Compiling and linking libev on Mac OS X

Yet another symbol(s) not found issue with Mac OS X. I wrote a C program that uses the libev event loop library that when compiled produces this output:
$ make
clang midnight.c midnight_logging.c -o midnight
Undefined symbols for architecture x86_64:
"_ev_default_loop", referenced from:
_main in midnight-Wlcawk.o
"_ev_io_start", referenced from:
_main in midnight-Wlcawk.o
"_ev_run", referenced from:
_main in midnight-Wlcawk.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [midnight] Error 1
I used homebrew to install libev. The shared library is in /usr/local/lib per normal and I've used every combination of compiler arguments including "-I /usr/local/lib", "-l libev" and "-L /usr/local/lib".
Assistance appreciated, I'd rather not have to statically compile.
But you do not link against libev! The compiler isn't a clairvoyant (nor is the linker), you have to tell it what to search for those symbols...
clang midnight.c midnight_logging.c -o midnight -lev

Apple Mach-O Link (Id) Error

My code is identical to this question I asked earlier so there's no point in duplicating it here
This is the error I am getting:
Undefined symbols for architecture x86_64:
"_stdscr", referenced from:
_screen_init in screen.o
"_werase", referenced from:
_screen_init in screen.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've had this error in the past and I feel like it has something to do with a duplicate function name or something, but I have no idea how to debug this.
EDIT:
After adding the libncurses.dylib file to my project, the errors discussed above have disappeared, but a new error has emerged when I call screen_init(); in my main.cpp:
// main.cpp
#include "screen.h"
int main(){
screen_init();
}
// new error
Undefined symbols for architecture x86_64:
"screen_init()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Pretty much the same error except now in my main. Is there another library I am missing? These errors are very mysterious and not very helpful.
I guess you need to add the library to your link phase, if you are going with XCode you should add it in project details:
choose project details
choose build phases tab
open link binary with libraries part
click the plus symbol
add libncurses.dylib to your project.

Resources