Undefined reference using Allegro 5 with mingw-w64 - c++

I'm trying to compile an Allegro 5 program on Windows 10 with mingw-w64.
I already had installed mingw-w64. Output from g++ --version is:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I downloaded windows binaries for Allegro 5 from https://github.com/liballeg/allegro5/releases (File: allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip) and unzipped the file into C:/allegro5 so now I have C:/allegro5/bin, C:/allegro5/include, C:/allegro5/lib.
A small test program:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
al_init();
return 0;
}
And finally the command I run to compile: g++ test.cpp -I"C:/allegro5/include" -L"C:/allegro5/lib" -lallegro (There is a lib file called liballegro.dll.a under C:/allegro5/lib)
But there are some problems while linking:
C:\Users\xxxx\AppData\Local\Temp\ccg5z97Y.o:test.cpp:(.text+0x1e): undefined reference to `al_install_system'
collect2.exe: error: ld returned 1 exit status
A) What may be the reason for this ?
B) What should I do to compile in a static way ? Is changing -lallegro to -lallegro-static enough ?

This:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
is one of the the 32-bit GCC variants provided by MinGW-W64. You are attempting to link the
32-bit code it generates with the 64-bit libraries provided in:
allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip
which will not work. Replace your compiler with the appropriate 64-bit variant x86_64-posix-seh

Related

How to configure g++ as the default compiler in Mac OS (M1)

So, I wanted to use some header files native to GNU C++:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
I read that in MacOS, gcc and g++ are both linked to clang. So, we had to install the gcc using homebrew and use that.
But after installing gcc using homebrew. When I run
g++ --version
I get
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
But running g++-12 --version I get:
g++-12 (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My VSCode runs g++ (Apple One) to compile C/C++ files. For the goal I wanted to accomplish at the start, I read that we need to have the g++ (installed using homebrew) do the compiling.
So, I ran the following commands:
cd /opt/homebrew/bin
ls -s g++-12 g++
But now, even when I compile the following code:
#include <iostream>
int main()
{
std::cout << 1;
}
I get the following error:
In file included from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/bits/postypes.h:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iosfwd:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ios:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ostream:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iostream:39,
from test.cpp:1:
/opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
compilation terminated.
Now, removing the link using rm g++ reverts back to my original configuration. But that configuration can't run the headers I requested at the start. Is there any way to solve this?
Most of the above have mostly been taken from here. But I didn't find any solution. But, I have seen people using the same and getting successful.
EDIT:
I found a website where there was a solution. It was to compile not using g++. Rather use
g++-12 --sysroot=$(xcrun --show-sdk-path)
When I used this, it solved the problem. Can someone explain why this happened?
The header error likely indicates the g++ you installed via Homebrew may not be compatible with the Apple Xcode version installed in the macos system directories.
The solution is probably to reinstall one or both packages.
EDIT:
g++-12 --sysroot=$(xcrun --show-sdk-path) changes the search path for system header includes from the default (which was probably set when homebrew installed g++) to the one provided by the Xcode SDK currently installed.

Can't cross-compile gdb with MinGW

I'm trying to cross-compile gdb so I could debug my Windows version of program.
My host system is Arch Linux(5.13.4-arch1-1 to be exact).
I'm using MinGW from the official repositories.
$ i686-w64-mingw32-gcc --version
i686-w64-mingw32-gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I downloaded the source for gdb from https://ftp.gnu.org/gnu/gdb/ (gdb-10.2.tar.gz).
I unpacked it, cd into dir and ran:
$ ./configure --host=i686-w64-mingw32
$ make
For long it compiled without any errors and then:
CXX source-cache.o
source-cache.c:37:10: fatal error: srchilite/sourcehighlight.h: No such file or directory
37 | #include <srchilite/sourcehighlight.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
And also although compiler already compiled gdbserver and other different programs, I can't run them.
Launching gdbserver.exe with wine gives:
$ wine gdbserver.exe
0024:err:module:import_dll Library libgcc_s_dw2-1.dll (which is needed by L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe") not found
0024:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe") not found
0024:err:module:LdrInitializeThunk Importing dlls for L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe" failed, status c0000135
Is it not statically linked?
Does anybody know how to fix it?
You can avoid the problem with the configure flag --disable-source-highlight if you can do without sourxe highlighting.

compiling simple hellow world with mingw g++

I am new to mingw/C++ and would like to compile and run the following code I found online:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Unfortunately, running a g++ helloworld.cpp -o helloworld.exe throws the following error:
In file included from c:\mingw\include\_mingw.h:73,
from c:\mingw\include\wchar.h:53,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\cwchar:44,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from helloworld.cpp:1:
c:\mingw\include\w32api.h:59:10: fatal error: sdkddkver.h: No such file or directory
#include <sdkddkver.h>
^~~~~~~~~~~~~
compilation terminated.
I have the impression this is related to Windows SDK and triggered by #include <iostream>. What is <iostream>, why is it important here and how can I get it to compile with g++?
EDIT
g++ version information
D:\TEMP\mingw_example>g++ --version
g++ (MinGW.org GCC-8.2.0-5) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Using SDL2 with g++ / MinGW crashes on launch

I am working on a game and recently made the transition from SDL 1.2 to SDL2 (kinda late to the party but heh). Despite having no compilation or linking error whatsoever, the program crashed on launch while stating (translated from French) "The application failed to start up correctly". At first I thought it was my own fault, but then I got suspicious and put together a quick SDL2 test, which indeed after flawless compilation crashed on launch.
Here is the code I've been using for this example (you can't make it more simple) :
#include <SDL.h>
int main(int argc, char *argv[])
{
return 0;
}
And the compilation line is g++ main.cpp -ISDL2-2.0.4\x86_64-w64-mingw32\include\SDL2 -LSDL2-2.0.4\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 -otest.exe -fpermissive -std=c++11 -Wno-write-strings -Wno-overflow
For what it's worth :
> g++ --version
g++ (x86_64-win32-seh, Built by MinGW-W64 project) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I'm running MinGW-w64 on Windows, as you can see. The OS is Windows 8.1.
Welp. I actually used the wrong SDL2.dll. I feel dumb now.

undefined reference to `pthread_mutex_trylock'

I have the following test program.
#include <iostream>
#include <cstdlib>
using namespace std;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char *argv[])
{
int iret;
iret = pthread_mutex_trylock( & mymutex );
cout << "Test2 !!! " << endl;
pthread_mutex_unlock( & mymutex );
return EXIT_SUCCESS;
}
If I compile it without adding the pthread library I get the error for unresolved error for pthread_mutex_trylock , but only for function pthread_mutex_trylock.
If I replace pthread_mutex_trylock with pthread_mutex_trylock the program is compiled and runnig well also without the -lpthread* option.
If I add the -lpthraed option to the compile commands all runs well
this run well : $ g++ test2.c -o test2 -lpthread
this warn unresolved : $ g++ test2.c -o test2
Example error output:
$ g++ test2.c -o test2
/tmp/ccU1bBdU.o: In function main':
test2.c:(.text+0x11): undefined reference topthread_mutex_trylock'
collect2: ld returned 1 exit status
If I replace the instruction iret = pthread_mutex_trylock( & mymutex );
with iret = pthread_mutex_lock( & mymutex ); the program compiles and run without error also if didn't add the pthread libarry to the compile command
I know that is right to have the unresolved error if I didn't use the -lpthread option , but why I have not the same unresolved error also for other pthread_ function ?
I'm using gcc 4.4.2 on fedora 12
$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Do some have some suggestion about the meaning of this unreference only for pthread_mutex_trylock ?
thanks for the help, Enzo
If you use pthread functions you should link your object files with -lpthread and not worry about whether symbols are included in libc.
The rationale behind this is said to be such: some time ago the stubs in libc were used when application that used threads was run on a system without threading support. On such system, pthread_* functions became linked to libc stubs that returned errors showing that there's no threading functionality. While on "threaded" systems they were linked to pthread library and worked correctly.
Obviously, pthread_mutex_trylock function appeared after the policy changed to linking with -lpthread. So there's no stub for it.
You should perform both the compilation and linkage with the -pthread option, to be portable. On some systems, the compilation will have specific flags added (e.g., -D_REENTRANT) with -pthread specified.
If you're curious to see what -pthread will do to your compile and link flags, run gcc -dumpspecs.