Making a linux shared library for ZGE

Any topic not related to ZGE.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Making a linux shared library for ZGE

Post by Ats »

I split up from the original topic 3D Physics with ZgeBullet because it's another problem that prevents me to run ZgeBullet on linux and has nothing to do with it.

In order to discover where the building/linking of ZgeBullet is having a problem, I started by creating a simple cpp shared library from scratch:

Code: Select all

// mylib.cpp 

#ifdef _WIN32
#define EXPORT extern "C" __declspec(dllexport)
#undef NULL
#define NULL nullptr
#else
#define EXPORT extern "C"
#endif

EXPORT int test_int() { return 123; }
EXPORT float test_float() { return 0.987; }
EXPORT char* test_str()
{
	char* ch = new char;
	ch = "ZGE";
	return ch;
}
I compile it and link it like this:

gcc -Wall -fPIC -I . -c mylib.cpp -o mylib.o
gcc -shared -o mylib.so mylib.o

Which is working nicely in ZGE.

---

Then I modify it the same way ZgeBullet library works with bullet3, by including external h files:

Code: Select all

// mylib.cpp

#ifdef _WIN32
#define EXPORT extern "C" __declspec(dllexport)
#undef NULL
#define NULL nullptr
#else
#define EXPORT extern "C"
#endif

#include "extlib.h"

EXPORT int test_int() { return 123; }
EXPORT float test_float() { return 0.987; }
EXPORT char* test_str()
{
	char* ch = new char;
	ch = "ZGE";
	return ch;
}
EXPORT int test_extlib_int() { return extlib_int(); }

Code: Select all

// extlib.h

#ifndef EXTLIB_H
#define EXTLIB_H
int extlib_int();
#endif

Code: Select all

// extlib.c

#include "extlib.h"
int extlib_int() { return 456; }
I compile the extlib, then mylib, and link everything to obtain the shared library:

gcc -Wall -fPIC -c libs/extlib.c -o extlib/extlib.o
gcc -Wall -fPIC -I ./libs -c mylib.cpp -o mylib.o
gcc -shared -o mylib.so libs/extlib.o mylib.o

Just to be sure I'm doing the -I inclusion right, everything about "extlib" is in a different folder called "libs".
It's going nice, but in ZGE, the external lib isn't recognized, crash the app and trace "./mylib.so".

At this point, I'm a bit clueless. So if anyone knows something about cryptic arguments I could have missed, any help is welcome :lol:


Just in case someone is into this, here's the three verbose I get:

Building extlib

Code: Select all

Using built-in specs.
COLLECT_GCC=gcc
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --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 --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --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=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --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 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-c' '-o' 'libs/extlib.o' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/cc1 -quiet -v -imultiarch x86_64-linux-gnu libs/extlib.c -quiet -dumpbase extlib.c -mtune=generic -march=x86-64 -auxbase-strip libs/extlib.o -Wall -version -fPIC -fasynchronous-unwind-tables -fstack-protector-strong -Wformat-security -fstack-clash-protection -fcf-protection -o /tmp/ccDlynaI.s
GNU C17 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (x86_64-linux-gnu)
	compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/9/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C17 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (x86_64-linux-gnu)
	compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: bbf13931d8de1abe14040c9909cb6969
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-c' '-o' 'libs/extlib.o' '-mtune=generic' '-march=x86-64'
 as -v --64 -o libs/extlib.o /tmp/ccDlynaI.s
GNU assembler version 2.35.2 (x86_64-linux-gnu) using BFD version (GNU Binutils for Debian) 2.35.2
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-c' '-o' 'libs/extlib.o' '-mtune=generic' '-march=x86-64'
Building mylib

Code: Select all

Using built-in specs.
COLLECT_GCC=gcc
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --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 --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --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=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --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 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-I' './libs' '-c' '-o' 'mylib.o' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/cc1plus -quiet -v -I ./libs -imultiarch x86_64-linux-gnu -D_GNU_SOURCE mylib.cpp -quiet -dumpbase mylib.cpp -mtune=generic -march=x86-64 -auxbase-strip mylib.o -Wall -version -fPIC -fasynchronous-unwind-tables -fstack-protector-strong -Wformat-security -fstack-clash-protection -fcf-protection -o /tmp/ccUgtFtU.s
GNU C++14 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (x86_64-linux-gnu)
	compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/9"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 ./libs
 /usr/include/c++/9
 /usr/include/x86_64-linux-gnu/c++/9
 /usr/include/c++/9/backward
 /usr/lib/gcc/x86_64-linux-gnu/9/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++14 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (x86_64-linux-gnu)
	compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 466f818abe2f30ba03783f22bd12d815
mylib.cpp: In function ‘char* test_str()’:
mylib.cpp:16:7: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   16 |  ch = "ZGE";
      |       ^~~~~
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-I' './libs' '-c' '-o' 'mylib.o' '-mtune=generic' '-march=x86-64'
 as -v -I ./libs --64 -o mylib.o /tmp/ccUgtFtU.s
GNU assembler version 2.35.2 (x86_64-linux-gnu) using BFD version (GNU Binutils for Debian) 2.35.2
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-fPIC' '-I' './libs' '-c' '-o' 'mylib.o' '-mtune=generic' '-march=x86-64'
Linking everything

Code: Select all

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --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 --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --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=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --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 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'mylib.so' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=/tmp/ccv3AxB8.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -shared -z relro -o mylib.so /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. libs/extlib.o mylib.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'mylib.so' '-mtune=generic' '-march=x86-64'

Edit:
If I reproduce the error I made in my last post on the ZgeBullet topic, by forgeting about adding mylib.o during the linking, it's working the same: ZGE recognizes mylib.so, but it's not working afterwards.
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Making a linux shared library for ZGE

Post by Ats »

Ok, writing everything down made me realize what was the problem. I needed to compile the extlib with g++ (or c++), not gcc.

g++ -Wall -fPIC -c libs/extlib.c -o libs/extlib.o
gcc -Wall -fPIC -I ./libs -c mylib.cpp -o mylib.o
gcc -shared -o mylib.so libs/extlib.o mylib.o

(it's even working without the -fPIC argument... Which is weird, maybe.)

I continue my researches.
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Making a linux shared library for ZGE

Post by Ats »

Back to ZgeBullet shared library. Now I know how to check what is linked in a shared library:
readelf -s ZgeBullet.so | grep FILE

So I tried adding every .o files from bullet3 plus ZgeBullet.o during the linking with:

ALL_O_FILES=$(find ~/bullet3-3.09/build_cmake/src/ -type f -name "*.o")
gcc -shared -o ZgeBullet.so ZgeBullet.o $ALL_O_FILES

But it's not working either. ZGE doesn't recognize the library.

And same as before, if I skip linking ZgeBullet.o, the library is recognized but doesn't work.


The thing is, I find an object file for every header needed by ZgeBullet.cpp, but the first one:

#include "btBulletDynamicsCommon.h"
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#include "BulletCollision/Gimpact/btGImpactShape.h"
#include "BulletCollision/CollisionDispatch/btGhostObject.h"
#include "BulletDynamics/Character/btKinematicCharacterController.h"
#include "BulletCollision/CollisionShapes/btShapeHull.h"

The build of bullet3 doesn't produce btBulletDynamicsCommon.o
Could that be coming from that?
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Making a linux shared library for ZGE

Post by VilleK »

Perhaps the .so file has a dependency to another .so file that it cannot locate? There should be a command to check what runtime dependencies a .so file has.
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Making a linux shared library for ZGE

Post by Ats »

I managed to find some good intel in the Android.mk file included in ZgeBullet project : https://github.com/Rado-1/ZgeBullet/blo ... Android.mk

:arrow: bullet3 version used by ZgeBullet is number 2.85.1
Rado1 build instructions are outdated and compiling ZgeBullet with old bullet3-2.83.7 returns a errors

:arrow: All bullet3 cpp files needed for the project are clearly listed.

But contrary to later versions of bullet3, linking the entire project on Linux gives me errors on those files:

Code: Select all

/usr/bin/ld: /home/ats/bullet3-2.85.1/build_cmake/src/BulletCollision/CMakeFiles/BulletCollision.dir/Gimpact/btGImpactShape.o: warning: relocation against `_ZGVZN11btMatrix3x311getIdentityEvE14identityMatrix' in read-only section `.text'
/usr/bin/ld: /home/ats/bullet3-2.85.1/build_cmake/src/BulletCollision/CMakeFiles/BulletCollision.dir/Gimpact/btGImpactShape.o: relocation R_X86_64_PC32 against symbol `_ZNK18btGImpactMeshShape19getGImpactShapeTypeEv' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
Then I compiled bullet3 again using -DCMAKE_POSITION_INDEPENDENT_CODE=ON argument on cmake in file build_and_run_cmake.sh

I didn't mention it before, but bullet3 examples are working fine with the above compilation.


But even after a long listing of only needed objects for the linking, following the list of cpp files in Android.mk, I get the same: a library recognized by ZGE, but its functions still aren't reachable.

int returns -288
float returns 84
string returns An unhandled exception occurred at $00000000004018E7:
EAccessViolation: Access violation
$00000000004018E7


The runtime dependencies of ZgeBullet.so gives nothing particular:

Code: Select all

readelf -d ZgeBullet.so

Dynamic section at offset 0x1c24b0 contains 25 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
By comparison, here's the same for sunvox.so:

Code: Select all

 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libasound.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libmvec.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Making a linux shared library for ZGE

Post by Ats »

:twisted: :twisted: :twisted: :twisted: :twisted:

The compiled lib was working all along... In fact my ZGE test got it's ZExternalLibrary / CallingConvention back to Cdecl at some point... What a waste of time :roll:

I'm going to clean up everything and make a simple tutorial to complete Rado1 building explanation of the project.

zgebullet_linux.png
zgebullet_linux.png (666.86 KiB) Viewed 8905 times

Edit: Ok, now both Cdecl and Stdcall are working on Rado1 demo... :roll:...

And maybe I've included too many objects. The .so file is 2.1Mo, while the .dll file is 700Ko.
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Making a linux shared library for ZGE

Post by VilleK »

Good you got it working :)

The calling convention setting is only relevant for 32-bit targets.
Post Reply