How to link a project with multiple files using MinGW? - c++

I've been learning to use visual studio code recently to code in C++ since Visual Studio 2019 were having alot of problems with the Windows SDK.Recently i've succefully link and run an SFML/C++ file on VS Code using MinGw and CMake Gui.Here's the 'Makefile' file that i used to link:
Makefile(no extension):
all:
g++ main.cpp -o main.exe -DSFML_STATIC -I F:\SFMLPrj\StarFirestc\Prefixes\include -L F:\SFMLPrj\StarFirestc\Prefixes\lib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -lfreetype
for the .cpp file(main.cpp) i've tried copy the sample code on https://www.sfml-dev.org/ to test the linking process and everything works fine.
But then when i try to include another header(of a class called 'Game')into to main.cpp and link the following errors occurs in MinGW:
F:\SFMLPrj\StarFirestc>mingw32-make all
g++ main.cpp -o main.exe -DSFML_STATIC -I F:\SFMLPrj\StarFirestc\Prefixes\include -L F:\SFMLPrj\StarFirestc\Prefixes\lib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -lfreetype
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\duch6\AppData\Local\Temp\cc2mmCAA.o:main.cpp:(.text+0x16): undefined reference to `Game::Game()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\duch6\AppData\Local\Temp\cc2mmCAA.o:main.cpp:(.text+0x21): undefined reference to `Game::Run()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\duch6\AppData\Local\Temp\cc2mmCAA.o:main.cpp:(.text+0x31): undefined reference to `Game::~Game()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\duch6\AppData\Local\Temp\cc2mmCAA.o:main.cpp:(.text+0x42): undefined reference to `Game::~Game()'
collect2.exe: error: ld returned 1 exit status
Makefile:2: recipe for target 'all' failed
mingw32-make: *** [all] Error 1
I have searched for 4 hour and still have no idea how to link 2 or more file using MinGW.Can anyone help me ?

Related

Where is the library for GUID in win32

I was trying the example code on "How to Play Media Files with Media Foundation", and I tried to compile the code with the following makefile:
LDFLAGS = -LC:\\pathToWindowsSDK\\Lib\\10.0.22000.0\\um\\x64 -lMfplat -lMfuuid -lUser32 -lOle32 -lShlwapi -lMf
default: winmain.o player.o
g++ winmain.o player.o -o a.exe
winmain.o: winmain.cpp player.h
g++ winmain.cpp -c
player.o: player.cpp player.h
g++ player.cpp $(LDFLAGS) -c
and I keep getting the following undefined references:
C:/Personal/Soft/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: player.o:player.cpp:(.rdata$.refptr.GUID_NULL[.refptr.GUID_NULL]+0x0): undefined reference to `GUID_NULL'
C:/Personal/Soft/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: player.o:player.cpp:(.rdata$.refptr.MR_VIDEO_RENDER_SERVICE[.refptr.MR_VIDEO_RENDER_SERVICE]+0x0): undefined reference to `MR_VIDEO_RENDER_SERVICE'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:4: default] Error 1
Both are undefined errors of GUID, is there a library that I should link?
I'm using MSYS g++ compiler, and Windows 11 SDK (10.0.22000). Thanks in advance!
Try adding -luuid (Visual C++ projects created through Visual Studio link to uuid.lib by default).

Unable to compile of C++ in JGrasp

I am trying to compile a simple Hello World program in C++ on jGrasp but I am getting the following error
----jGRASP exec: g++ -g -o jGHello.exe jGHello.cpp -lglu32 -lfreeglut -lopengl32
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lfreeglut
collect2.exe: error: ld returned 1 exit status
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
I suppose that jGrasp is unable to compile the program due to -lfreeglut being missing. But I am unable to find any mention of it on the internet.
Compiler cant find -lfreeglut, its could be cause two reasons:
It doesnt exits (look if its already install)
Its not on libs path, then u ve to install it on libs path, or tell C++ compiler where is this lib, u can do it like -L /pathToLib
Ej: g++ -g -o jGHello.exe jGHello.cpp -lglu32 -lfreeglut -lopengl32 -L /pathToLib

Libnoise don't want to compile: Cannot find -lnoise

I'm trying to use the Libnoise library and I keep get this error:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -lnoise -static-libgcc-static-libstdc++ Resources/icon.res
f:/portableapps/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lnoise
collect2.exe: error: ld returned 1 exit status
I have the libnoise.lib in my Libs folder. I tried with -llibnoise instead, and I get:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -llibnoise -static-libgcc -static-libstdc++ Resources/icon.res
Compiled/Generator.o:Generator.cpp:(.text+0x3d): undefined reference to `noise::module::Perlin::GetValue(double, double, double) const'
Compiled/Generator.o:Generator.cpp:(.text+0x8a): undefined reference to `noise::module::Perlin::Perlin()'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0xa): undefined reference to `vtable for noise::module::Perlin'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0x19): undefined reference to `noise::module::Module::~Module()'
collect2.exe: error: ld returned 1 exit status
I don't know how to manually compile the source, it always return me a WinMain#16 error, no matter what I try to make.
Now, I really don't know what to do...
Anyone have a solution?
-l option of gcc supposes that library got name starting with lib, so -lnoise would link libnoise.
Usually .lib is a n import library or static library. The problems related to import libraries described here: How do import libraries work and why doesn't MinGW need them?
In relation to libnoise: Netbeans C++ using MinGW and the libnoise library

Undefined reference to pthread for Gtest

I have been scratching my head since yesterday trying to make gtest work but I just can't fix it after reading the links below.
undefined reference to `pthread_key_create' (linker error)
error during making GTest
The compilation error displayed is this:
g++ main.o tests.o var.o -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs
-lgtest -lgmock -lpthread -o test
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
My Makefile is:
CXXFLAGS=-I ../gmock/include -I ../gmock/gtest/include
test:main.o tests.o var.o
g++ $^ -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs -lgtest -lgmock -lpthread -o $#
I am still in the process of learning Linux, compiling and linking source files.
The Error is about linking error on pthread.
You have to make the pthread flag to -pthread and the follow CXX should do the trick.
LDLIBS = -L../gmock/lib/.libs -L../gmock/gtest/lib/.libs -lgtest -lgmock
test:main.o tests.o var.o
g++ -isystem $(LDLIBS) -pthread $^ -o $#
This link have a good Makefile which addresses all your problems and it follows general standards on creating a Makefile

Why do I get this error when I compile a C++ program in jgrasp?

----jGRASP exec: gcc -g -o C1.exe C1.c -lglu32 -lfreeglut -lopengl32`
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lfreeglut
collect2.exe: error: ld returned 1 exit status
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
I am using mingw32 compiler and I followed the instructions from the link below:
http://users.csc.calpoly.edu/~akeen/courses/csc101/references/gcc.html
Hope that someone knows what to do
thank you
You probably need to specify the location of the freeglut library with the flag -L /path/to/freeglut, so compile as
gcc -g -o C1.exe C1.c -L/path/to/freeglut -lglu32 -lfreeglut -lopengl32
or, if you use an IDE, there should be an option to specify the location of the libraries used by the linker.
See http://users.csc.calpoly.edu/~phatalsk/references/jGrasp_gcc_Setup/GCC_JGrasp_Install.html
Settings/Compiler Settings/Workspace, add the flag to C/L field.