gcc : undefined reference to 'function' [duplicate] - c

This question already has answers here:
gcc: undefined reference to
(2 answers)
Closed 5 years ago.
I have installed a C library on my computer (Ubuntu) called xx, whose header file is at /usr/local/include/xx/xx.h and the .a file and .so file are in the path /usr/local/lib named libxx.a and libxx.so.
The test file:
#include<xx/xx.h>
#include<stdio.h>
int main(){
printf("Test\n");
call_function_declared_in_the_header_file();
return 0;
}
When I use gcc to compile gcc test.c -o test, I get the following error:
/tmp/ccb7O0eh.o: In function `main':
test.c:(.text+0xa): undefined reference to `call_function_declared_in_the_header_file'
collect2: error: ld returned 1 exit status
I don't know why. I am not good at the C language. So how can I fix it?

you have to link against the library, i.e. if you want to include <math.h> ( libm.so / libm.a ) you have to link against :
gcc program.c -o program -lm where the -lm invokes the linking
see http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html,
https://www.rapidtables.com/code/linux/gcc/gcc-l.html
compiling always involves the compiler and the linker

Related

how do i resolve this error when compiling C code that includes SDL [duplicate]

This question already has answers here:
how to use SDL in linux?
(3 answers)
Closed 10 months ago.
when i try to compile the following C code with gcc -I/SDL2-2.0.20/x86_64-w64-mingw32/include hsdl.c -L/SDL2-2.0.20/x86_64-w64-mingw32/lib in windows
#include "SDL2/SDL.h"
int main( int argc, char* args[] ) {
SDL_Init( SDL_INIT_EVERYTHING ); //Start SDL
SDL_Quit(); //Quit SDL
return 0;
}
i get the following errors
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PC\AppData\Local\Temp\ccfFajdG.o:hsdl.c:(.text+0x15): undefined reference to `SDL_Init'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PC\AppData\Local\Temp\ccfFajdG.o:hsdl.c:(.text+0x1a): undefined reference to `SDL_Quit'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
i did install SDL headers and library files manually by moving the files to the library and include paths.
You need to tell gcc where to find those include files and where to find required libraries for SDL2.
Compile your program like this:
gcc `pkg-config --libs --cflags sdl2` ./hsdl.c -o ./hsdl.exe
If you haven't installed SDL2 on msys2 use this command:
pacman -S mingw-w64-x86_64-SDL2
If you haven't installed pkg-config on msys2 use this command:
pacman -S mingw-w64-x86_64-pkg-config

Undefined reference to pow when compiled using gcc [duplicate]

This question already has answers here:
Undefined reference to `sin` [duplicate]
(4 answers)
Closed 1 year ago.
I am using pow function in C and included the math.h library too
The following is the error that I am getting :
/usr/bin/ld: /tmp/ccUkOL31.o: in function `main': a1B.c:(.text+0xf3):
undefined reference to 'pow' collect2: error: ld returned 1 exit
status
Although I read on StackOverflow that linking is required. But that's weird. Is there any way to avoid that extra step when we are compiling with gcc. g++ seems to be doing it automatically.
Is there any way to avoid that extra step when we are compiling with gcc?
The way I do it is having a script that invokes gcc (I have ~/bin in my SPATH)
$ cat ~/bin/mycc
#!/bin/sh
gcc -std=c11 -pedantic -Wall $* -lm
$ mycc example1.c example2.c

ceil() only works for rvalues [duplicate]

This question already has answers here:
Why does the order of '-l' option in gcc matter? [duplicate]
(3 answers)
Closed 3 years ago.
I'm trying to compile this program in ubuntu 18.04, 64 bits:
#include <math.h>
#include <stdio.h>
int main() {
double x = 1.9;
float y = 1.8;
int x2 = ceil(x);
int y2 = ceil(y);
printf("%d, %d\n", x2, y2);
return 0;
}
The gcc command I'm using is:
gcc -std=c99 -lm main.c -o main
And the error I'm obtaining is:
/tmp/ccWL94J9.o: In function `main':
main.c:(.text+0x30): undefined reference to `ceil'
main.c:(.text+0x41): undefined reference to `ceil'
collect2: error: ld returned 1 exit status
Although, if I replace ceil(x) by ceil(1.2) for example, and something similar for ceil(y), I can build and execute the program.
In addition, I have checked that I do have libm.so installed:
bash> find /usr/lib -name "*libm.so*"
/usr/lib/x86_64-linux-gnu/libm.so
What I'm missing?
With the following line, it compiles:
gcc -std=c99 main.c -o main -lm
(putting -lm after -o main)
See https://stackoverflow.com/a/11894098/4030665
With some expressions E, particularly constants, the compiler can evaluate ceil(E) while it is compiling, and it does so if this optimization is not disabled. Then the compiler generates code that uses the result and does not call ceil. When the compiler fails to evaluate ceil(E) during compilation, it generates code that calls ceil.
The switch -lm is an abbreviation for the standard math library. The linker processes input files in the order they appear on the command line. When the linker processes a library, it extracts from the library all object modules that contain a definition for a symbol that is currently needed (referenced but not defined) in the executable file (or other output) it is building.
GCC maintains the order of the various units on its command line. Given -lm main.c, it compiles main.c to product an object module, and then it passes -lm and the object module to the linker in that order. Since the math library is first, when the linker processes it, it has not yet seen any symbols that reference it, and therefore it does not take any modules from the library.
If GCC is instead given main.c -lm, the linker processes the math library after the object module for main. Then, when the linker is processing the math library, it will know that main references ceil, so it will extract from the math library the module that defines ceil.
Thus gcc -std=c99 -lm main.c -o main will work with source code that uses only constants with ceil but will not work with the source code in the example, but gcc -std=c99 main.c -lm -o main will work.

Compile a C program in Linux using shared library [duplicate]

This question already has answers here:
linker tells me it can't resolve symbols, but they're there?
(3 answers)
Closed 7 years ago.
I am trying to compile a simple C program in Linux with a shared library.
I have all together in the same folder the following files:
mymain.c
#include "myclib.h"
int main() {
func();
return 0;
}
myclib.h
void func();
myclib.c
#include <stdio.h>
void func() {
printf("hello world!!!!!!!!!!!!\n");
}
Then I followed these steps:
gcc -c fPIC myclib.c (create memoryaddress independent objectfile)
which produces: myclib.o
gcc -shared -fPIC -o libmyclib.so myclib.o (create shared library)
gcc -c mymain.c (creates an object file out of main.c)
So far so good - then I have the following files ready:
main.o
libmyclib.so
So I try to create a program out of this syntax:
gcc -o program -lmyclib -L. mymain.o
(I guess the prefix lib from libmyclib should be replaced with an l?)
But I get the error message from the gc-compiler:
*mymain.o: In function `main':
mymain.c:(.text+0xa): undefined reference to `func'
collect2: error: ld returned 1 exit status*
I have also tested this syntax:
gcc -o program mymain.c -L -lmyclib -Wl,-rpath,.
Then I get the following error:
/usr/bin/ld: cannot find -lmyclib.so
collect2: error: ld returned 1 exit status
What am I doing wrong in these two implementations?
How do I compile this program to an executable using shared library?
You need to place -l options on the end of linker invocation command line:
gcc -o program -L. mymain.o -lmyclib

undefined reference to `sqrt' [duplicate]

This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 3 years ago.
Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
You have to add the -lm flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
Link with -lm to link with the math library

Resources