This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 4 years ago.
I use mingw32 to compile some C++ code. After trying some things and a lot of search I have a questions : The flags orders seems very important thought I found barely nothing that explain that on the internet. For exemple :
g++ main.cpp -L/lib -I/include -lmingw32 -lSDL2 -lSDL2main -Wall
-g -o main.exe -> Compile
g++ main.cpp -L/lib -I/include -lmingw32 -lSDL2main -lSDL2 -Wall
-g -o main.exe -> Not compile
g++ main.cpp -L/lib -I/include -lSDL2 -lSDL2main -lmingw32 -Wall
-g -o main.exe -> Compile
I can understand why -lSDL2main need -lSDL2 first but so why the place of -lmingw32 is not important ?
EDIT :
I explain : from what I know of compilation process .cpp come first, -l indicate clearly libraries (last step of compilation). So why if I put main.cpp to the end it's continue compilation process and finish with error from the library. What mingw use if he don't have any source file, he just compile lib ?
It's not the order of the fags, it's the order of the libraries. The last libraries should be the ones that everyone requires, then the second to last is the one that the last doesn't need, etc.
Why libmingw32 is not required is probably because the SDL libraries don't need it.
Related
This question already has answers here:
Run-time linking to dynamic libraries not on LD_LIBRARY_PATH
(1 answer)
What's the difference between -rpath and -L?
(1 answer)
Closed 2 years ago.
This is the g++ command running as reported by make:
g++ -o "../build/HeatMethod" obj/Debug/HeatMethod/GradientLerp.o obj/Debug/HeatMethod/MRS.o obj/Debug/HeatMethod/heat_method.o -L../libraries/glfw-3.2.1/bin -L/usr/local/opt/libunwind-headers/include -L../libraries/vulkansdk-linux/1.2.154.0/x86_64/lib -L../libraries/shaderc/build/libshaderc -L../libraries/googletest/build/lib -L../libraries/benchmark/build/src -L../build -L../libraries/GeographicLib/BUILD/src -lstdc++fs -O0 -ldl -export-dynamic -lstdc++fs -O0 -ldl -pg -export-dynamic ../build/libCoreVulkanEngine.a -lglfw -lunwind -lvulkan -lshaderc_combined -lpthread -lfreetype -lharfbuzz -lcairo -lgtest -lbenchmark -lGeographic
Might be somewhat verbose but focus on the fact that -L../libraries/GeographicLib/BUILD/src is specified in the command.
The linker fails with the following error:
./HeatMethod: error while loading shared libraries: libGeographic.so.19: cannot open shared object file: No such file or directory
Well, here are the directory contents:
ls libraries/GeographicLib/BUILD/src/
CMakeFiles cmake_install.cmake libGeographic.so libGeographic.so.19 libGeographic.so.19.1.0 Makefile
The file it claims does not exist is defined inside the directory specified in the command, why cannot it find it?
Cleaned directory through make -C Generated/ clean (Generated is where the makefile is)
Someone helped me get things working by doing:
LD_PRELOAD="/home/makogan/vkengine/libraries/GeographicLib/BUILD/src/libGeographic.so" ./HeatMethod
This question already has answers here:
Building glew on windows with mingw
(8 answers)
Closed 3 years ago.
I'm a daft C/C++ novice using Windows, building with MinGW through console. I have been looking for days on how to build GLEW so that I can statically link it with my incredibly simple SDL+OpenGL program. For static linking, GLEW supposedly requires a special lib apart from lglew32 called lglew32s, and this is what I cannot get my hands on.
I'm trying to learn to use gcc/g++ right now, and to understand the options and the whole preprocess/compile/link thing in general. I'm about 5% there. I found some GLEW building batch file examples around the net, most being pretty old, but even one of them that was new didn't compile lglew32s for me, so I can dynamically link but I can't statically link.
So because I can't find glew32s.lib anywhere online built with anything other than VS, I have to learn to understand what this means and somehow figure out how to compile glew32s with what I've learned from it:
gcc.exe -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc.exe -fno-builtin -fno-stack-protector -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib
ar.exe cr lib/libglew32.a src/glew.o
gcc.exe -DGLEW_NO_GLU -DGLEW_MX -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c
gcc.exe -fno-builtin -fno-stack-protector -shared -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib
ar.exe cr lib/libglew32mx.a src/glew.mx.o
I have learned to compile my own .cpp and .c projects using gcc/g++ (again I'm barely out of the "hello world" phase in terms of actual code), and also how to dynamically link them to the SDL and GLEW libraries. I understand very well now what this all means:
g++ sdlglew.cpp -o sdlglew -Wall -lmingw32 -lSDL2main -lSDL2 -lglew32 -lopengl32 -I "C:\SDL2-2.0.9\i686-w64-mingw32\include\SDL2" -L "C:\SDL2-2.0.9\i686-w64-mingw32\lib" -mwindows
(My SDL stuff is off in its own directory. GLEW's headers and libs are in MinGW's dir)
I think I need either -DGLEW_STATIC in my command OR #define GLEW_STATIC in my sdlglew.cpp to compile GLEW statically, but those options with lglew32 give me no errors upon compile and fail to start due to the missing glew32.dll. Compiler spits out errors with -static because SDL has not been set up for it yet.
Basically, if you have any resources on understanding what exactly the mentioned compile script is doing per line and how I could build glew32s, I'd be real happy :S I want to understand it, but it really seems like there's no documentation on it for us newbies
Can't you use the Makefile? (It should be included in the MinGW evironment, I think)
In which case I think you should just be able to do something along the lines of:
cd thefolderwithmakefile
make all GLEW_STATIC=1
Then you need to define GLEW_STATIC ( as you said by -DGLEW_STATIC or by #define GLEW_STATIC )when you are building your project files as well.
This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 years ago.
There are linker errors to Symbols defined by SFML, but i cannot see how they occur despite that I linked the lib.
I'm using make, which I currently learn and I want to build a minimalistic dev-environment with it.
Give a holler if you need anymore information than the following. I'd just like to minimize the questions size.
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make clean
rm -f build/*.o build/release/*.o build/debug/*.o build/test/*.o
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make tests
//test obj first
g++ -std=c++14 -Wall -pthread -Iinclude -c test/Packager.ut.cpp -o build/test/Packager.ut.o -g3
//now the src obj
g++ -std=c++14 -Wall -pthread -Iinclude -c src/ClientAddress.cpp -o build/debug/ClientAddress.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Packager.cpp -o build/debug/Packager.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Package.cpp -o build/debug/Package.o -g3
Built debug object files.
//now the first test itself
g++ -std=c++14 -Wall -pthread -Iinclude -lsfml-network build/test/Packager.ut.o build/debug/ClientAddress.o build/debug/Packager.o build/debug/Package.o -g3 -o bin/test/Packager.ut
build/test/Packager.ut.o: In function `main':
/home/XXX/Documents/dev/cpp/proj/beep/test/Packager.ut.cpp:69: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::ClientAddress::ClientAddress()':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:21: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::operator==(nw::udp::ClientAddress const&, nw::udp::ClientAddress const&)':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:33: undefined reference to `sf::operator==(sf::IpAddress const&, sf::IpAddress const&)'
...
and so on ... every mentionings of sf:: inside the files are quoted
I get the same error pattern if I try to compile the other tests (for ClientAddress for example)
Of course i now want to know what i linked wrong how there. As you can see the lib is linked with -lsfml-network. I also checked the SMFL installation, so it is at least less likely a lib file gone missing from standard directory.
I guess there is an error in my usage of g++ , compiling and linking orders or smth.
My project tree:
>bin
----mainexec
--->test
----.ut
>build
--->debug
----.o
--->release
----.o
--->test
----.ut.o
>src
---- .cpp
>include
---- .h
>test
---- .ut.cpp
As a second part of the question, I'd like to ask if there is a better way to build tests, because I simply link every src-obj with my test-obj, even if there are way more src-obj linked than it actually needs. It should work and I do not have to maintain my test dependencies all the time, which later would be very cumbersome. What is common ?
sfml-network has a dependency on sfml-system. Try add -lsfml-system before -lsfml-network in your linker command in your makefile
I'm trying to cross-compile a small test opengl/glew program and I get linker errors from undefined references.
$ /usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib/ -lglfw -lglew32 -lopengl32 main.cc
/tmp/cct8OpVh.o:main.cc:(.text+0x50): undefined reference to `glfwInit'
/tmp/cct8OpVh.o:main.cc:(.text+0xa6): undefined reference to `glfwOpenWindowHint'
...
The same code does work when compiling for linux:
$ g++ -I/usr/include -L/usr/lib/ -lglfw -lGLEW -lGL main.cc
One thing that caught my eye is that every exported symbol from cross-compiled libraries has an extra underscore prefix:
$ nm /usr/lib/libglfw.a | grep glfwInit$
00000000 T glfwInit
$ /usr/i486-mingw32/bin/nm /usr/i486-mingw32/lib/libglfw.a | grep glfwInit$
00000000 T _glfwInit
This seems to be a common thing since even libstdc++.a shares this property, but why is my cross-compiler linker then looking for non-underscore symbols?
Running arch with following packages (local means AUR):
community/mingw32-binutils 2.23.1-3
community/mingw32-gcc 4.7.2-1
local/mingw32-glew 1.9.0-1
local/mingw32-glfw 2.7.7-1
community/mingw32-pthreads 2.9.1-1
community/mingw32-runtime 3.20-4
community/mingw32-w32api 3.17-1
EDIT
After playing out with both pkg-config and watching glfw recompile and test itself, I came up with the following magic that seems to work, at least I'm compiling:
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -mwindows main.cc -lglew32 /usr/i486-mingw32/lib/libglfw.a /usr/i486-mingw32/lib/libopengl32.a -static-libgcc
There are few questions though:
What is the difference between linking with -l and without?
Why do I need to use -l with glew and cannot with glfw
I was able to solve my problem and, in case someone ever runs into similar situation hope this helps you.
There are two versions of glew32 -library in my system, glew32.a and glew32.dll.a.
glew32.a does not allow for using --static, glew32.dll.a does.
The two commands which compile succesfully, only first of which I've run are:
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32.dll -lglfw -lopengl32 --static
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32 -lglfw -lopengl32
Looking at my old compiling attempts, the problem was wrong order of libraries and that main.cc was after the libraries.
There is a program, called pkg-config that helps you to configure your compiler. See the manual pages for usage information, but, for this case, its output is:
-mwin32 -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -lglfw -lglu32 -lopengl32 -lm -s -mwindows -e _mainCRTStartup
Try to compile with this, I guess it will work.
I'm working on a collaborative project and the guy who controls the master branch of the project works on a mac. Because of this, there are a few minor differences in his makefile that don't translate well to me on Windows. I use MinGW to hold my libraries, and compile using g++.
Makefile (as I thought it should go)
bomb: source/Level.cpp source/Level.h source/main.cpp
g++ -o bomb source/Level.cpp source/main.cpp -Wall -I. -I\MinGW\include\SDL -lSDLmain -lSDL -lSDL_image
The Makefile that works for him:
bomb: source/Level.cpp source/Level.h source/main.cpp
g++ -o bomb source/Level.cpp source/main.cpp -Wall -I. -I/Library/Frameworks/SDL.framework/Headers -lSDLmain -lSDL -lSDL_image -framework
My include files for the SDL library are at "C:\MinGW\include\SDL". Using that knowledge, how would I correctly write the makefile? As a side note, the error I get when using my current makefile is:
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to 'WinMain#16'
Have you tried adding -lmingw32 to the end of the libraries list? And do you have a int main(int argc, char** argv) function defined somewhere? Also, see the SDL FAQ