A
A
Alex Serov2014-06-29 22:33:55
MinGW
Alex Serov, 2014-06-29 22:33:55

"undefined reference" error when linking in MinGW?

I made an object file from a C source file using MinGW.
When trying to link:
ld -o test.exe test.o
gives an error "undefined reference". For example, to printf.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-07-15
@gibsonman01

The function code printfis in the library libmsvcrt.a. To be able to use printf, you need to link test.owith libmsvcrt.a. However, the matter is not limited to this, in order to at least run the program, a lot of auxiliary code is needed. The linker options might look something like this for a normal "hello world":

ld -LC:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0
-LC:/MinGW/x64/mingw64/bin/../lib/gcc
-LC:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/lib/../lib
-LC:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../lib
-LC:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/lib
-LC:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../..
C:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o
C:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/crtbegin.o
test.o
-lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32
-luser32 -lkernel32 -liconv -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt
C:/MinGW/x64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/crtend.o

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question