How to link allegro5 library to C++ project - c++

I have got a C++ project written in CodeBlocks but I do not know how to link it with allegro5. I want to make people able to compile my project without installing any libraries or packages. So I put allegro directory downloaded from their webpage into project folder. Next I included library
#include "allegro/include/allegro5/allegro5.h"
But when I try to compile I have got an error
/home/stanek/ClionProjects/proj/guiMain.cpp:17: undefined reference to `al_install_system'
/home/stanek/ClionProjects/proj/guiMain.cpp:22: undefined reference to `al_create_display'
/home/stanek/ClionProjects/proj/guiMain.cpp:28: undefined reference to `al_map_rgb'
/home/stanek/ClionProjects/proj/guiMain.cpp:28: undefined reference to `al_clear_to_color'
/home/stanek/ClionProjects/proj/guiMain.cpp:30: undefined reference to `al_flip_display'
/home/stanek/ClionProjects/proj/guiMain.cpp:32: undefined reference to `al_rest'
/home/stanek/ClionProjects/proj/guiMain.cpp:34: undefined reference to `al_destroy_display'
This is my guiMain.cpp:
#include <iostream>
#include "allegro/include/allegro5/allegro5.h"
using namespace std;
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
And this is my CMakeList.txt
cmake_minimum_required(VERSION 3.3)
project(proj)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11")
set(SOURCE_FILES guiMain.cpp)
add_executable(projids ${SOURCE_FILES})
UPDATE:
I have changed set in my CMakeList
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 '")
but I have got an error:
Error:Configuration proj [Debug]
Compiler exited with error code 1: /usr/bin/c++ -xc++ -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 ' -g -v -dD -E
c++: error: pkg-config --cflags --libs allegro-5.0 : No such file or directory
Using built-in specs.
COLLECT_GCC=/usr/bin/c++
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-5-20160209/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 5.3.0 (GCC)
and
Error:Configuration proj [Release]
Compiler exited with error code 1: /usr/bin/c++ -xc++ -g -Wall -std=c++11 'pkg-config --cflags --libs allegro-5.0 ' -O3 -DNDEBUG -v -dD -E
c++: error: pkg-config --cflags --libs allegro-5.0 : No such file or directory
Using built-in specs.
COLLECT_GCC=/usr/bin/c++
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-5-20160209/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 5.3.0 (GCC)

Actually, it is possible to allow others to play your game without intalling the allegro libraries on their system.
One approach is to statically link your executable to static versions of the
allegro libraries. The allegro wiki actually has a
tutorial
on how to do this with Code::Blocks.
In short, static linkage includes the necessary parts of Allegro directly in
your executable so the user doesn't have to worry about linking when they run
it.
Another option is to use dynamic linkage, but package the needed dynamic
libraries along with your game. For example,
your game folder might look like:
bin/
my_game.exe
liballegro.dll
liballegro_color.dll
...
Then the user can link to the libraries you have provided without worrying about what is installed on their system.
Note that dynamic libraries typically end with .so (Linux) or .dll (Windows) while static libraries end with .a.
Edit:
Just read you said compile and not run. If you want them to compile it themselves, you will need to include the libraries or instruct them on how to get the libraries themselves.

Related

Using gcc compile some package statically got an error about libprocps.a

# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-27ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
Well, I'm trying to compile my code staitcally with gcc:
g++ build/test.o -o build/test -std=c++11 -DCURVE_MNT4 -DNO_PROCPS -I ./src/ -static -lm -lff -lstdc++ -lgmpxx -lgmp -lprocps
that code works perfectly fine if you just run the same command without "-static" flag, but because of the project need, we have to compile this code statically, so we have to use "-static" flag and here are some logs:
# g++ build/test.o -o build/test -std=c++11 -DCURVE_MNT4 -DNO_PROCPS -I ./src/ -static -lm -lsnark -lff -lstdc++ -lgmpxx -lgmp -lprocps
......
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(pwcache.o): In function `pwcache_get_group':
(.text+0x1c8): warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(pwcache.o): In function `pwcache_get_user':
(.text+0x58): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(readproc.o): In function `sd2proc':
(.text+0x1233): undefined reference to `sd_pid_get_machine_name'
(.text+0x1247): undefined reference to `sd_pid_get_owner_uid'
(.text+0x1296): undefined reference to `sd_pid_get_session'
(.text+0x12b1): undefined reference to `sd_session_get_seat'
(.text+0x12c7): undefined reference to `sd_pid_get_slice'
(.text+0x12dd): undefined reference to `sd_pid_get_unit'
(.text+0x12f3): undefined reference to `sd_pid_get_user_unit'
collect2: error: ld returned 1 exit status
Makefile:22: recipe for target 'build/test' failed
make: *** [build/test] Error 1
any idea guys?
I hope there is a way to solve this problem, the "gcc " way or "ar -r" way or an alternative to “libprocps.a” is fine to me, I just want to know how can I compile successfully with flag "-static", thank you all
Recompiling libprocps without systemd support (--with-systemd=no) got rid of the systemd dependency and allowed #Will_Z to create a fully statically linked binary.

Prefer sysroot headers over headers in the toolchain using gcc

I'm trying to cross-compile a simple code snippet
1 #include <sys/socket.h>
2 #include <stdio.h>
3
4 int main()
5 {
6 printf("%d\n", SOL_NETLINK);
7 return 0;
8 }
with arm-linux-gnueabihf-g++ (from Ubuntu) against Raspbian sysroot with --sysroot switch set to $SYSROOT
The compilation fails with this error:
test.cpp:6:20: error: ‘SOL_NETLINK’ was not declared in this scope
Even though $SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h contains
the needed define.
So I figured that toolchain contains the mentioned header as well and it's included first. The latter header somehow doesn't have this SOL_NETLINK define. So I need a way to tell the compiler to prefer the toolchain's headers to sysroot's ones.
> arm-linux-gnueabihf-g++ -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabihf/5/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-armhf-cross/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-armhf-cross --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-armhf-cross --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libgcj --enable-objc-gc --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --disable-werror --enable-multilib --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --includedir=/usr/arm-linux-gnueabihf/include
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4)
#include <sys/socket.h> will include a file which is under the folder {include path}/sys/socket.h.
Include path can be any folder that you add with various options like gcc -I or --includedir. In addition there are some default include path, which you should be able to see with gcc -xc++ -E -v -.
Using gcc "-I" option will put the include path to search first, before any of the default include paths.
Now, this should give you enough information to understand that the include you give can't point to the file $SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h as it isn't end with {include path}/sys/socket.h
Use -isystem flag with $SYSROOT/usr/include/arm-linux-gnueabihf
Albeit GCC can resolve triple (arm-linux-gnueabihf part) in the path, but it won't do so with --sysroot option.

Linker Error cutest binary with matlab

I need to implement a library with mathematical problems to test an algorithm I wrote in matlab. The library is called "cutest" (see here: http://ccpforge.cse.rl.ac.uk/gf/project/cutest/wiki/).
As I am using Ubuntu, it's not possible to install this thing via the homebrew repo. Therefore I was doing the manual installation and was able to install cutest and the problemdecoder "sifdec" (also downloadable ob the page above).
I am now stuck in the point, where I need to compile a binaryfile that can be used by matlab. I guess it has something to do with the compiler flags of fortran/gcc and a 32/64 bit thing (as I was already scrolling through other posts and google).
The Error I get is the following:
/usr/bin/ld: i386 architecture of input file `/home/beck/CUTEst/cutest
/objects/pc64.lnx.gfo/double/mcutest.o' is incompatible with i386:x86-64
output
/usr/bin/ld: /home/beck/CUTEst/cutest/objects/pc64.lnx.gfo/double
/mcutest.o: file class ELFCLASS32 incompatible with ELFCLASS64
/usr/bin/ld: final link failed: File in wrong format
collect2: error: ld returned 1 exit status
mex: link of ' "mcutest.mexa64"' failed.
The mex file is from matlab, I didn't modify it yet (nor the optionsfile for mex).
The settings for the makefile "pc64.lnx.gfo", which I created (automatically) via a terminal installation are the following:
# Architecture dependent makefile
# (automatically generated by install_cutest)
VERSION = pc64.lnx.gfo
# Basic system commands
CP = /bin/cp
MV = /bin/mv
RM = /bin/rm -f
SED = /bin/sed
GREP = /bin/grep
AR = /usr/bin/ar
RANLIB = /usr/bin/ranlib
# Directory for binaries
PRECIS = double
OBJ = $(CUTEST)/objects/$(VERSION)/$(PRECIS)
OBJS = $(CUTEST)/objects/$(VERSION)/single
OBJD = $(CUTEST)/objects/$(VERSION)/double
MOD = $(CUTEST)/modules/$(VERSION)/$(PRECIS)
SEDS = $(CUTEST)/seds/$(PRECIS).sed
MVMODS = $(MV) -f $(OBJ)/*.mod $(MOD)/ || true
# Compiler options
FORTRAN = gfortran-4.7
BASIC = -c -fno-second-underscore -fPIC
MODULES = -I$(MOD)
OPTIMIZATION = -O
NOOPTIMIZATION = -O0
DEBUG =
OPENMP = -fopenmp
F77 = -ffixed-form
F90 =
F95 =
NOFMAIN =
USUAL =
SPECIAL =
F77SUFFIX = f90
F95SUFFIX = f90
TIMER = GEN
NOT95 = IS95
NOT64 = IS64
AMPLDIR =
CC = gcc-4.7
CCBASIC = -c -ansi -pedantic -fPIC
CCONDEF =
CCDEBUG =
CCFFLAGS = -lgfortran
# Special flags
# Libraries
BLAS =
LAPACK =
CUTESTUSED =
# Shell used
BINSHELL = sh
# Set directories for optional packages
include $(CUTEST)/src/makedefs/packages
# Body of makefile
include $(PWD)/makemaster
I'd really appreciate any kind of help, this issue keeps me from going further with my master thesis.
EDIT: Added outputs of gcc-4.7/gfortran-4.7 -v
gcc-4.7 -v:
Using built-in specs.
COLLECT_GCC=gcc-4.7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.4-3ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.4 (Ubuntu/Linaro 4.7.4-3ubuntu3)
gfortran-4.7 -v:
Using built-in specs.
COLLECT_GCC=gfortran-4.7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.4-3ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.4 (Ubuntu/Linaro 4.7.4-3ubuntu3)
I was able to solve the problem with the help of an academic member of my university. In case anyone has/will have the same problem here is what worked for me:
As the toolchain was 64-bit and the only file that was generated in 32-bit was the mcutest.o the error had to be in combination with the mex compilation (cause only this file was compiled with the mex compiler of matlab).
Inside the folderstructure was a file called makemaster which holds the settings alredy choosen in the installation progress for gfortran and gcc, but also settings for mex (which i wasn't asked to modify yet). However, after some testing, adding the line CFLAGS='$(CFLAGS)' in the MFLAGS resulted in a 64-bit output file and the error didn't occur anymore.
Thanks for your help anyway!

How to link gfortran when I compile?

I use ubuntu 12.04, my Makefile FC:gfortran, but I couldn't compile,
I checked on it, it installed(my gcc upgraded from 4.6.3 to 4.7)
How do I link to it?
I tried pkg-config --cflags --lib gfortran or gtkxxx, it says not in the pkg_config_path
I checked gfortran: COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' -with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
You don't need pkg-config to use gfortran; you just need to compile and link with the gfortran executable (and you can also use gfortran to link C code with Fortran code, and you could probably link using gcc with -lgfortran)
BTW, pkg-config accepts the --list-all option; with the command
pkg-config --list-all |grep -i gtk
you can understand that GTK is in fact gtk+-x11-3.0
You should also read the documentation of GFORTRAN; it has a chapter about mixed-language-programming
Of course you need to install the gfortran, gfortran-4.7 and libgfortran-4.7-dev packages

Not works compilation c++ 4.7.2 in Debian testing x64

I'm trying to do a manual compilation with g++ in debian testing x64bits,
The content of my file test.cpp is:
#include <iostream>
using namespace std;
int main() {
cout << "Hola Mundo" << endl;
return 0;
}
but when I run the command, for example to compile a simple hello word
g++ test.cpp -o test
When I run the command ls:
test.cpp
g++ generates nothing. Is rare.
Basically happens with any program I try to compile with c++.
Datas about my Debian:
kernel: 3.9-1-amd64
Datas about g++( run the command g++ -v):
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with- bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)
How to fix my trouble?
Thank you.
Have you tried to add the compiler-commands for more detailed info?
-Wall -g
Maybe it works if you hit that ?
g++ -o test test.cpp