#include <math.h>
int main() {
nan("");
}
The above code works on Linux with gcc fn.c -lm. It doesn't on solaris-sparc. How do I fix this?
The error message is:
Undefined symbol nan, first referenced in file /var/tmp//ccsdneUZ.o ld: fatal: Symbol referencing errors. No output written to test collect2: ld returned 1 exit status.
Related
So I'm currently new to C right now, and I just downloaded the cs50 library. I tried to make an input by using get_int(), but it's not working. Here's my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int i = get_int("Input: ");
printf("Output: %i\n", i);
}
This is the error that I got
cc calculator.c -o calculator
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in calculator-225c65.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: *** [calculator] Error 1
I am trying to use a 3rd party library (.so) file in my project. A very simple program to do that:
#include "LibGent.h"
#include "genttypes.h"
int main() {
GT_LIB* gtLib = LibGentGet();
return 0;
}
I compile/link the program above against libgent.so:
gcc testing.c -L/home/username/proj -lgent
I get the following error:
/tmp/ccALOJwU.o: In function `main': testing.c:(.text+0x11): undefined reference to `LibGentGet'
collect2: error: ld returned 1 exit status
However, I can see that the function LibGentGet is defined in the .so file:
readelf -Ws libgent.so | grep "LibGentGet"
301: 0000000000044820 8 FUNC LOCAL DEFAULT 8 LibGentGet
What am I doing wrong?
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a simple program as
demo_use.c
#include "libhello.h"
int main(void) {
hello();
return 0;
}
libhello.h
void hello(void);
libhello.c
#include <stdio.h>
void hello(void) {
printf("Hello, library world.\n");
}
I have used the command in terminal as
gcc demo_use.c -o test
error
Undefined symbols for architecture x86_64: "_hello",
referenced from: _main in ccZdSQP3.o
ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
You need to compile both the source files together to generate the binary. use
gcc demo_use.c libhello.c -o test
Otherwise, the definition of hello() function will be absent. So, at linking time, linker will throw undefined symbol error.
I'm using the gsl library in a c code. Everything goes fine but when i use the command gsl_linalg_cholesky_invert the terminal shows the following message:
Undefined symbols for architecture x86_64:"_gsl_linalg_cholesky_invert", referenced from:
_main in cc4eefuQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
A code example is the following.
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_linalg.h>
#include <math.h>
void main()
{
gsl_matrix * A = gsl_matrix_calloc (2,2);
gsl_matrix_set(A,0,0,1);
gsl_matrix_set(A,0,1,0.5);
gsl_matrix_set(A,1,0,0.5);
gsl_matrix_set(A,1,1,1);
gsl_linalg_cholesky_decomp(A);
gsl_linalg_cholesky_invert(A);
}
If i comment out the line gsl_linalg_cholesky_invert(A);, everything goes fine.
I compile the code with the following command:
gcc-mp-4.7 wrapnorm.c -o wrapnorm -lgsl -l gslcblas -lm
with a macbook pro with mountain lion and gsl version 1.9.
Thanks
I am using Cygwin environment with Lua Interpreter package included while cygwin installation.
So I am able to compile and run sample lua progs.
But when i try to execute a sample c file which has lua calls , i am always getting this following error.
$ cc -o ../samples/ctest -Wall ../samples/ctest.c
/tmp/ccOYgLj4.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
My sample ctest.c file contents:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* lua interpreter */
lua_State* l;
int main () {
int dofile;
/* initialize lua */
l = lua_open();
/* load lua libraries */
luaL_openlibs(l);
/* run the hello.lua script */
dofile = luaL_dofile(l, "hello.lua");
if (dofile == 0) {
/* call foo */
lua_getglobal(l,"foo");
lua_call(l,0,0);
}
else {
printf("Error, unable to run hello.lua\n");
}
/* cleanup Lua */
lua_close(l);
return 0;
}
hello.lua file contents:
print("from c hurray")
on searching the net everywhere they say some linker error and have to include -llua51. So i tried the following .
$ cc -o ../samples/ctest -Wall -llua5.1 ../samples/ctest.c
/tmp/cc3v5Nim.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
Vedhashree#Vedhashree-PC /cygdrive/c/cygwin/bin
$ ls /usr/lib/liblua*.a
/usr/lib/liblua.a /usr/lib/liblua5.1.a
/usr/lib/liblua.dll.a /usr/lib/liblua5.1.dll.a
Can you help me fix this issue and make my first embedded lua c program work?
Update:
$ cc -o ctesing -Wall ctesting.c -llua5.1
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua5.1
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua51
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua51
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua
collect2: ld returned 1 exit status
-----------------------------------------------------------------
Still I get only these errors :(
Place -llua5.1 after ../samples/ctest.c. Objects should be linked in reverse order of dependency.
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua5.1
UPDATE: Your update describes a different problem. In this case the linker cannot find a liblua5.1.a file in its search path. Make sure that you have such a library on your system and try adding its path using the -L option.