问题
I had some trouble getting MinGW to recognise libraries for CSFML. I've had to manually link each library using the linker. Now I dont have the standard library that gets linked automatically from a simple gcc call.
bbroo@DESKTOP-1F1J3SM ~/Shaders
$ ld shaders.o libcsfml-system.a libcsfml-window.a libcsfml-graphics.a csfml-system-2.dll csfml-window-2.dll csfml-graphics-2.dll
shaders.o:shaders.c:(.text+0x10): undefined reference to `__main'
shaders.o:shaders.c:(.text+0x4bc): undefined reference to `sleep'
What is the name of the standard library in MinGW and how do I link to it in LD?
I know this doesnt answer the question, but compiling with this works:
gcc shaders.c libcsfml-system.a libcsfml-window.a libcsfml-graphics.a csfml-system-2.dll csfml-window-2.dll csfml-graphics-2.dll
回答1:
Using 'ld' in not obvious. Since gcc successfully compiles your project, you can reproduce linking stage directly with 'ld' with the following steps.
- Compile with 'gcc' with -v option added. This will produce a lot of text, you may want to redirect it to file with '2>out'
- Find the last line that begins with 'COLLECT_GCC_OPTIONS='
- After this line there will be a list of gcc option which regulate behavior of 'ld'. From this line extract the following: two -L options which point to main library folder and gcc-specific library folder; after that a sequence of used object modules and libraries. Copy these to your 'ld' command line. The list is rather large. The order of objects and libraries is important!
Here is the sample command line which successfully links simple test program:
ld -Lc:\MinGW\lib\gcc\mingw32\5.3.0 -Lc:\MinGW\lib -o test.exe crt2.o crtbegin.o test.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt crtend.o
You may need to check paths, copy *.o files to your folder or add path to them and add your libraries.
来源:https://stackoverflow.com/questions/44361841/manually-link-standard-library-in-mingw