Reputation: 3905
I am trying to link a large project with GCC 4.8.1 from MinGW for a x86 target. I am calling the linker like this
D:\MyGCCPath\gcc -L [LIBPATHS] -nostdlib -Wl,-Map,D:\PathToMapFile.map,--emit-relocs [OBJECTFILES AND LIBS] -lmsvcrt -lgcc -o D:\PathToMyOutputFile
With this call I get this linker rror:
libgcc.a(__main.o):(.text+0x5a): undefined reference to `atexit'
I tried different msvcr
versions (100 and 90), but this was more a desperate attempt, since I am not very familiar with this problem. I am using the correct libraries provided by MinGW.
Is there any way I can fix this error?
Upvotes: 0
Views: 4595
Reputation: 736
The reasoning in your comment chain with Yu Hao is completely correct.
You need to import libmsvcr120.a
with -lmsvcr120
to get the atexit
symbol.
You may also need some of these, depending on what symbols you reference, but this is speculation based on very briefly grepping through the source code in clang/lib/Driver/ToolChains
for the string OPT_nostdlib
:
lgcc_eh
lgcc_s
lgcc
lgomp
liomp5md
lmingw32
lmingwex
lmingwthrd
lmoldname
lmsvcr100
lmsvcr110
lmsvcr80
lmsvcr90
lmsvcrt-os
lomp
lssp_nonshared
lssp
In general, you can find which libraries contain the symbols you're looking for by calling MinGW's nm.exe
(or GNU nm
on Linux) on all of the libraries you have (in x86_64-w64-mingw32\lib
for example), and piping the output to a text file. You'll get a few hundred thousand lines of text like this that you can search through:
libmsvcr120_defs01591.o:
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 i .idata$4
0000000000000000 i .idata$5
0000000000000000 i .idata$6
0000000000000000 i .idata$7
0000000000000000 t .text
0000000000000000 I __imp_atexit
U _head_lib64_libmsvcr120_def_a
0000000000000000 T atexit
The T atexit
(uppercase T
) denotes the symbol being exported, with its definition in the .text
section.
If you see U atexit
, that means "I don't know where atexit
is, but it's needed here". If an import library only contains lines with U atexit
, then the definition is not in that import library.
You'll notice atexit
also defined by libmsvcr120d.a
, which is the debug version of libmsvcr120.a
, and defined by libmsvcr120_app.a
, which is (from what I gather) the version you would link against when distributing an executable on the Windows App Store.
Upvotes: 0
Reputation: 3675
Libraries are checked in the order used on the command line so use -lgcc -lmsvcrt
.
Upvotes: 0
Reputation: 122463
You are linking with -nostdlib
, and atexit()
is a function from stdlib.h
.
According to GCC Link Options:
-nostdlib
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as
-static-libgcc
or-shared-libgcc
, are ignored.
Upvotes: 4