Undefined symbols for architecture x86_64 "_min" - c

I'm having a problem using min() and max() function in my C project. I've imported math.h, but when I compile the file I keep getting the following error (a similar error is displayed even using gcc instead of llvm):
Undefined symbols for architecture x86_64:
"_min", 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)
I think the problem is that there isn't a 64 bit library of math.h... or the compiler can't find it. I'm using Mac OS X 10.7. How may I fix this problem?

Even though I can see "min" defined in libSystem.dylib, I don't think there's an exported header for that. And I can't figure out where "_min" is coming in from, in terms of the include files.
Normally "min" is referred to with a macro or with your own function. Check out this very related question somebody else asked a while back.
If you look in math.h, there are some "min" type functions in there but they are for floats and doubles. If you are just working with integers or custom types, roll your own function.

Related

How to fix " Undefined symbols for architecture x86_64 " in 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

GNU Archimedes fails to build -- Undefined symbols for architecture, even though they are present

I'm rather unused to C, and completely new to compiling bigger projects, and I'm having problems trying to build GNU Archimedes.
I tried the ./configure and make approach, as well as simply executing gcc -lm archimedes.c -o archimedes in the src/ directory, as the documentation suggests. Both give a similar error message:
Undefined symbols for architecture x86_64:
"_MM2", referenced from:
_ParabMEP2D in ccwORAXj.o
_Hole_MEP2D in ccwORAXj.o
"_creation", referenced from:
_EMC in ccwORAXj.o
"_rnd", referenced from:
_MCdevice_config in ccwORAXj.o
_scat in ccwORAXj.o
_EMC in ccwORAXj.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
MM2, rnd, and creation are functions contained in one of many .h files in src/. Every solution to similar sounding problems had issues linking multiple compiled .c files together, but in this case, there's only one .c file.
If it's relevant, the name of the .o file in the error changes with every try.
I've tried building it under macOS (with both clang and gcc, the latter installed via Homebrew) and Fedora Linux, with similar results.
What am I doing wrong? I hope this is the right place to ask, but I have no idea where else to turn. I'm grateful for suggestions.
Thanks in advance and have a good day!
In the folder /archimedes-2.0.1/src/ go through all header files (.h) and change every "inline" to say "static inline".
This fix was found by Alexander Vogt. His original post is linked to below:
https://lists.gnu.org/archive/html/archimedes-discuss/2017-11/msg00000.html
The problem has to do with compatibility issues between older and newer versions of c compilers.

MATLAB Generated C Code Compilation Error

I have a MATLAB function that has signal processing and machine learning and I wanted to test it on another OS like linux. So I use codegen to generate C code for that function. When I come to run it (predictActivityFromSignalBuffer) on the command line, this is what I get:
MacBook-Pro-2:predictActivityFromSignalBuffer kareem$ gcc predictActivityFromSignalBuffer.c
Undefined symbols for architecture x86_64:
"_featuresFromBuffer", referenced from:
_predictActivityFromSignalBuffer in predictActivityFromSignalBuffer-1a1886.o
"_main", referenced from:
implicit entry/start for main executable
"_mynn", referenced from:
_predictActivityFromSignalBuffer in predictActivityFromSignalBuffer-1a1886.o
"_rtIsNaN", referenced from:
_predictActivityFromSignalBuffer in predictActivityFromSignalBuffer-1a1886.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What is this error telling me exactly? I have no idea how to debug this or where to start/look.
Did you really just call
$ gcc predictActivityFromSignalBuffer.c
without any other dependencies? Usually there come a lot of other files with a code generation that have to be linked.
I do not know what you have done to produce your .c file. I recommend to use the coder wizard by typing
coder
in the Matlab command window. This will guide you through the whole process of code generation and also offers a lot of support and testing possibilities. You can also choose to compile your functions to ready-to-use DLL libraries or executatables. Just have a look at it.

embedding Julia in C/C++ on OSX

I am trying to compile a very simple C/C++ program to call Julia functions. Following the instructions that you find on the Julia documentation page, I set up my link path to /Users/william.calhoun/Desktop/romeo/lib/julia looking for libjulia.so and I set up my include path to /Users/william.calhoun/Desktop/romeo/include/julia looking for julia.h
I have a C file called test.c which runs the following code:
#include <stdio.h>
#include "skeleton.h"
#include <julia.h>
int main(int argc, const char * argv[]) {
jl_init(NULL);
/* run julia commands */
jl_eval_string("print(sqrt(2.0))");
/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook();
return 0;
}
However this yields the following error:
Undefined symbols for architecture x86_64:
"_jl_atexit_hook", referenced from:
_main in test.o
"_jl_eval_string", referenced from:
_main in test.o
"_jl_init", referenced from:
_main in test.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 am not doing anything other than calling functions defined properly (hopefully) within the Julia source code. What am I doing wrong? This seems like the simplest example and I can't figure it out.
Any help would be much appreciated!
Linking to libjulia (libjulia.dynlib on OS/X)
This error is a result of not linking to libjulia, as all of the symbols (_jl_atexit_hook, _jl_eval_string, _jl_init) are located in that library. Broadly, for all 3 of the following platforms (Windows, OS/X, Linux), the approach is similar, and though the location of the libjulia library is different on Windows than the other 2 this stackoverflow question is applicable. Also to be completely accurate, on OS/X, dynamic libraries have the extension .dynlib not .so as they do on Linux.
The link step
For simplicity, assuming you've compiled to object code (there is a file called embed.o), here's the link step.
cc -o embed embed.o -L/Users/william.calhoun/Desktop/romeo/lib/julia -Wl,-rpath,/Users/william.calhoun/Desktop/romeo/lib/julia -ljulia
There are 2 important things to note here.
Linking using -ljulia will allow the linker to resolve all of the above symbols.
Since this is a dynamic library and that dynamic library is located in a non standard location (e.g. not in /usr/lib), the dynamic linker will not be able to find it at run time unless you give it special instructions on how to find it. The -rpath directive causes the linker to insert the path /Users/william.calhoun/Desktop/romeo/lib/juliainto the list of paths to search.

C/OSX/clang confusion: "symbol(s) not found" happening at link time instead of compile time

I have a question, but I'm not sure if it's about C, clang or OSX.
When I compile a simple GLUT program like so:
clang test.c -framework OpenGL -framework GLUT
And I intentionally insert a function call that doesn't exist, like so:
thisFunctionIsNotDefinedAnywhere();
I get an error, as expected. But here's the rub -- I don't get the error until link time!
Undefined symbols for architecture x86_64:
"thisFunctionIsNotDefinedAnywhere" referenced from:
_main in test-e099d2.o
ld: symbol(s) not found for architecture x86_64
Why is this? Is this because pre-C99 there were implicit declarations? I've been programming for a long time and have never run into this before. Is it because I've been spoiled on GCC and MSVC which (I seem to remember) cause a compiler error in this situation? Or does it have to do with how framework linking works in OSX, which I am new to?
I appreciate any clarification!
As expected, this gives a warning with a recent version of clang. I tried it with the version that comes with Xcode 6.1, and got this compiler output:
$ clang test.c
test.c:2:5: warning: implicit declaration of function 'thisFunctionIsNotDefinedAnywhere' is invalid in
C99 [-Wimplicit-function-declaration]
thisFunctionIsNotDefinedAnywhere();
^
1 warning generated.
Undefined symbols for architecture x86_64:
"_thisFunctionIsNotDefinedAnywhere", referenced from:
_main in test-376416.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Since using undeclared functions is legal in C, the compiler message can't be an error. But it does show a clear warning, without specifically enabling warnings with command line options.
Because of implicit function declaration, if you enable warnings then you would be warned that thisFunctionIsNotDefinedAnywhere(); is implicitly declared, returning int by default.
If you add a function prototype, then the warning will be gone, but then at the link stage, the compiler wont find the function definition, and issue the error.

Resources