MinGW undefined link to malloc, free, sprintf, _beginthreadex

I am using MinGW. I have code that calls malloc and several other general-purpose functions. When I type:

gcc TestCode.c

I get a.exe file, it works fine, and I don't get any warnings.

If I type this:

gcc -c TestCode.c -o TestCode.o
ld *.o

I get a whole bunch of warnings, such as:

TestCode.o:TestCode.c:(.text+0xa): undefined reference to `__main'
TestCode.o:TestCode:(.text+0x2e): undefined reference to `printf'
TestCode.o:TestCode:(.text+0x42): undefined reference to `_strerror'
TestCode.o:TestCode:(.text+0x69): undefined reference to `snprintf'
TestCode.o:TestCode:(.text+0x7e): undefined reference to `malloc'
TestCode.o:TestCode:(.text+0x93): undefined reference to `_strerror'
TestCode.o:TestCode:(.text+0xb1): undefined reference to `sprintf'
TestCode.o:TestCode:(.text+0xcf): undefined reference to `free'

I assume this is a problem with what I call the linker. This way, I will only send the code if it is not clear what the problem is. I hope this is easy to fix, and I just forgot to include some super obvious libraries when linking.

+5
source share
2 answers

, ld . , C libc. gcc , , :

gcc -c TestCode.c -o TestCode.o
gcc *.o

ld , C libc. (, libcrt libc):

ld *.o -lcrt -lc
+7

, gcc, , - ( , ).

, , , , CRT Windows ( ). MinGW :

crt2.o
crtbegin.o

-ladvapi32 
-lshell32 
-luser32 
-lkernel32 
-lmingw32 
-lgcc 
-lmoldname 
-lmingwex 
-lmsvcrt 

crtend.o

--verbose, , gcc .

+1

All Articles