Error "undefined reference" while linking in MinGW - linker

I have got the object-file from source code using MinGW.
But on linking:
ld -o test.exe test.o
I get errors, for example the following:
undefined reference to printf

First, why are you using ld directly?
The following is an excerpt from the "GCC and Make" tutorial found at http://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html.
Compile and Link Separately
The above command compile the source file into object file and link with other object files (system library) into executable in one step. You may separate compile and link in two steps as follows:
// Compile-only with -c option
> g++ -c -Wall -g Hello.cpp
// Link object file(s) into an executable
> g++ -g -o Hello.exe Hello.o
Note g++ (you can substitute gcc if you are using C and not C++) is used both for compiling and linking. ld is not used at all.
The benefit of using g++ or gcc to link is that it will link with default libraries, such as the one you need to link with for printf, automatically.
To link with other libraries, you specify the library name with the -l parameter, as in -lmylib.

We can view commands ran by compiler via command
c99 -v test.o
We'll get some text. All after string which contains "COLLECT_CGG_OPTIONS" will be arguments of ld.
But size of executable file is much more then size of file got by previous way.

Related

usr/bin/ld: cannot find -lcurl: No such file or directory

I am trying to create a dynamic library, but I keep getting this error when I insert the command bellow on terminal:
gcc -o libvetoraleatorio.so -shared vetoraleatorio.o -lcurl
Then I get
/usr/bin/ld: cannot find -lcurl: No such file or directory
collect2: error: ld returned 1 exit status
That's the instructions that I am following that were provided by my teacher in college:
**Creating a dynamic library
**
Generate the source code from the dynamic library
-fPIC option (to generate position-independent code, i.e. code that works correctly no matter what memory location it is inserted into)
Use the -shared GCC option to generate the .so file
EXAMPLE:
$ gcc -c -fPIC -o aula18-apuracao.o aula18-apuracao.c -lcurl
$ gcc -o libaula18-apuracao.so -shared aula18-apuracao.o -lcurl
$ gcc -o aula18-apuracao aula18-main.c -L${PWD} -laula18-apuracao -lcurl
Note: -lcurl is because the example uses libcurl. The -L is to inform where the newly generated .so is. In this case, it is stating that it is in the current directory
Note: the library will not be included in the executable; it will be dynamically linked to the program at runtime
Note: the ldd command shows the dynamic libraries that will be loaded into an executable

libtiff build undefined reference to `_imp__TIFFOpen' error

probably this is a trivial newbie question, however, I can't figure out how to solve it.
I'm trying to build a test program using libtiff (test program copied from here). I've downloaded the static library libtiff.lib as well as the required header file tiffio.h. When I compile the main c function with no problem I have a main.o file. When I try to link main.o with libtiff using this command
gcc -g -Wall -o test.exe ./libtiff.lib ./test.o
I have this error:
undefined reference to `_imp__TIFFOpen'
I've looked into the lib file with nm -A libtiff.lib command and I can find this line
libtiff.lib:libtiff3.dll:00000000 I __imp__TIFFOpen
but it has 2 leading underscores instead of 1 as required by the linker. I'm using mingw on Windows 7 and all the required files are in the same directory.
No clue how to link with no errors.
Thanks in advance.
As suggested in the the comments, it was sufficient to invert the order of objects passed as arguments:
gcc -g -Wall -o test.exe ./test.o ./libtiff.lib

Using a static library in C

I found a useful library on github for my project, after building this later I tried to use some predefined function on it. I couldn't compile my project because there is some header file missing like this one :
In file included from main.c:2:0:
ptask.h:11:19: fatal error: ptime.h: No such file or directory
I compiled my project using this command :
gcc main.c -L. -lptask
This is all the files in project folder :
libptask.a main.c ptask.h
This is the library content:
$ ar -t libptask.a
pbarrier.c.o
pmutex.c.o
ptask.c.o
ptime.c.o
rtmode.c.o
tstat.c.o
libdl.c.o
dle_timer.c.o
calibrate.c.o
Do I need to add all the headers of this files or just link the lib when compiling ?
Your main.c #include-s ptask.h which in turn #include-s ptime.h. Having compiled static libs alone is not enough (that's the linker's job), you still need to have all used header files (which is the compiler's job), both the ones you use and their dependencies, recursively applicable.
Normally you need to be sure that the header files are in your "include path", something that a lot of compilers define with -I as a command-line option. You'll need to include the source directory of that library, or if it has a make install option, then the place where they got installed.
regarding:
gcc main.c -L. -lptask
this is performing the compile step and the link step in one command.
It is also not enabling the warnings, which should always be enabled during the compile step.
Suggest something similar to the following to compile
gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -g -c main.c -o main.o -I.
and when you have fixed all the warnings, then use something similar to the following to link
gcc main.o -o main -L. -lptask

Split generation of a Shared Object library (.so) file into two pass instead of one

I am trying to build a shared library lib_test.so from 'test.c' & test.exp files. This lib_test.so file will be used as a extension to another application.
The application doc specifies generation of tle lib_test.so file directly in a single pass by the following command:
`gcc -q64 -o lib_test.so test.c -bM:Sre -bE:test.exp -bnoentry`
But my requirement is to build the library in two passes:
Compile to generate test.o file using gcc command.
Link to generate the library lib_test.so using ld command.
I tried this as follows:
Executed compile step as follows: gcc -q64 -c -o test.o test.c.
Create lib_test.so as follows: ld -bM:Sre -bE:test.exp -bnoentry -o lib_test.so test.o
But it is not generating a proper lib_test.so file.
I am using Ubuntu 16.04 LTS 64-Bit with latest GCC
Can you please suggest the correct way to split the process into two passes...
Thanks & Regards.
You rarely ever want to use ld to perform the linking. The gcc frontend does the better job of setting the right flags etc. So, use gcc.
i.e. Instead of
ld -bM:Sre -bE:test.exp -bnoentry -o lib_test.so test.o
do
gcc -bM:Sre -bE:test.exp -bnoentry -o lib_test.so test.o
in your second step.
The big difference between linking with the GCC frontend program gcc and with the actual linker ld is that the GCC frontend adds a few libraries to be linked with. Most notably the GCC runtime library (-lgcc_s or -lgcc) and the actual standard C library (-lc).
When you invoke ld directly you do not tell it to link with those libraries.
There might also be other libraries and flags the GCC frontend passes to ld without your knowledge. For the "one pass" build, pass the flag -v to gcc for verbose output and see what arguments, flags and libraries it uses.

gcc in Windows cannot compile C program written for Unix/Linux

I am a Unix/Linux newbie who is trying to run a shell script written by a person who left no documentation and has since demised. This script contains line:
./search $opt1 $arg1 < $poly 2>&1 | tee $output
Which is trying to get the file $poly and call program ./search and divert the output to $output.
When I get to this line, I am given message: ./search: cannot execute binary file: Exec format error
search is a C program called from the script and is in the same folder as various other C programs to do with this project. Script and C programs were developed and originally executed on a Unix/Linux box which is no longer available, so I have been asked to try to resurrect this project but under Windows using gcc in NetBeans and cygwin.
The message : ./search: cannot execute binary file: Exec format error is most likely to do with the fact there is no executable file for search. When I try to build the C programs I get the following output:
C:\cygwin64\bin\make.exe -f Makefile
gcc -ansi -g -c cbuild.c
gcc -ansi -g -c complex.c
gcc -ansi -g -c mylib.c
gcc -ansi -g -c poly.c
gcc -ansi -g -c real.c
gcc -ansi -g -c zero.c
gcc -lgmp -lm -lrt -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o
real.o: In function `rabs':
/cygdrive/c/../progs/real.c:9: undefined reference to `__imp___gmpf_abs'
/cygdrive/c/../progs/real.c:9:(.text+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp___gmpf_abs'
real.o: In function `radd':
I assume that R_X86_64_PC32 refers to the environment I am using. I am using a 64 bit version of Netbeans with gcc 5.4.0 in a 64 bit version of cygwin on Windows 10.
Can anyone advise what I must to to resolve this so that I can build the C programs?
The problem is this:
gcc -lgmp -lm -lrt -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o
By default, the linker will link libraries and objects in the order specified on the command line, and, when linking a library, will only include symbols needed by things before it on the command line. Since -lgmp is first, there are (as yet) no outstanding symbols (except main), so nothing is included from the library. When later objects need the symbols from it, they won't see them.
Change the order to
gcc -o cbuild cbuild.o complex.o mylib.o poly.o real.o zero.o -lgmp -lm -lrt
and it should work. Alternately, use the -Wl,--as_needed linker option to get the linker to remember earlier libraries and relink them if more symbols from them are referenced by later object files (requires a recent version of the GNU linker -- I have no idea if it works with cygwin).
This kind of misordering is usually a symptom of a broken Makefile. The normal Makefile structure has a bunch of variables that are set to control the default rules that know how to compile source files and link object files. The two variables relevant for linking are LDFLAGS and LDLIBS, and the difference is that LDFLAGS comes before all the object files on the command line and LDLIBS comes after all the object files.
So in order to make things work, you need to ensure that all of the -l options and other libraries are in LDLIBS:
LDLIBS = -lgmp -lrt -lm
and NOT in LDFLAGS

Resources