Geant4 static libraries to export executable

Good morning,
I’m in trouble with STATIC libraries.
I need to run my program on a computing centre (or on other computers) and libraries need to be “attached” to the executable.
Geant4 uses for default global (shared) libraries, stored in the temporary directory. I understood from one last post
(http://hypernews.slac.stanford.edu/HyperNews/geant4/get/installconfig/1799.html?inline=1)
that i need to install Geant4 with the cmake argument BUILD_STATIC_LIBS enabled
and then using in CmakeLists.txt the command find_package(Geant4 REQUIRED static).

However following these tips my code compiles using global libraries.
Below the code used. Where I am wrong?

during Geant4 installation, in the build folder:

cmake -DBUILD_SHARED_LIBS=OFF .   
cmake -DBUILD_STATIC_LIBS=ON .
make 
make install

in CmakeLists.txt of my code folder

option(Geant4_static_FOUND "TRYING TO BUILD STATIC LIBRARIES" ON)
if(DBUILD_STATIC_LIBS)
  find_package(Geant4 REQUIRED static)
else()
  find_package(Geant4 REQUIRED)
endif()

The first cmake invocation should be o.k., can you confirm that you have the .a files present under where you installed Geant4?

In the CMake fragment, this should be something like, assuming you always want to use static libs:

find_package(Geant4 REQUIRED static)

or if you want to choose:

if(BUILD_STATIC_LIBS)
  find_package(Geant4 REQUIRED static)
else()
  find_package(Geant4 REQUIRED)
endif

Hi,
first of all thank you for your response.

Yes, there are .a files in the Geant4 install folder for each libxxxx.so file.
So I assume that cmake BUILD_STATIC_LIBS=ON . worked, even if no cout confirmation was printed…

After installation i used

option(BUILD_STATIC_LIBS ON)
if(BUILD_STATIC_LIBS)
  find_package(Geant4 REQUIRED static)
else()
  find_package(Geant4 REQUIRED)
endif()

or better
find_package(Geant4 REQUIRED static)

but the problem is unsolved. With cmake compilation the cout was:

Creating shared library /home/adminlab/geant4_workdir/tmp/Linux-g++/MyExecutable/libMyExecutable.so ...
Using global libraries ...
Linking MyExecutable
... Done!

I don’t know what to do more…

The compilation output on the last step indicates that the build used a GNUmakefile rather than the CMake generated one. What are the contents of the directory where make (for MyExecutable) was run, and what were the steps taken to run cmake then make?

the folder contents, related to the compilation are:

CMakeFiles/ (folder)
cmake_install.cmake
CMakeCache.txt
CMakeLists.txt (where I wrote the code related to BUILD_STATIC_LIBS)
MakeFile
GNUmakefile (but is not modificed by months, i don't think it is used)

I use:

cmake -DGeant4_DIR=/home/adminlab/Geant4/geant4.10.00-install/lib/Geant4-10.0.4/ /home/MyPathCode/
make

Thanks! I think make is probably taking the GNUmakefile in preference to the MakeFile generated by CMake. If you’re not using the GNUmakefile, then remove it and make should then pick up the CMake makefile.

1 Like

I can’t believe it. To avoid these kind of situations I use my compile.exe which deletes all the cmake files EXCEPT GNUMakeFile… Now it works! You’re right.

I tried to execute MyExecutable.exe on the computing centre and unfortunately it request other libraries not linked statically, in

/home/adminlab/Geant4/geant4.10.00_StaticLibraries-install/lib/Geant4-10.0.4/

and however these files request other libraries in

/usr/lib/...
/lib64/...
...

it cannot be proposed to upload GBs of .so library files too…

The problem is that the computing centre doesn’t accept installations and sudoers privileges…

I thought that linking the /home/adminlab/geant4_workdir/tmp/Linux-g++/MyExecutable/libxxx.so files was enought to make my executable portable.
Do you have any advice for me?

However,
really thank you again!

A totally static binary is do-able, though may be tricky to get perfectly right. The instructions here:

offer some guidance, the primary things would appear to be:

  1. Build Geant4 with -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DCMAKE_FIND_LIBRARY_SUFFIX=".a". Your local system will have to have the static versions of any dynamic libraries it’s picking up in /usr/lib and /usr/lib64.
  2. When configuring your executable, pass cmake the flags -DCMAKE_EXE_LINKER_FLAGS="-static" -DCMAKE_FIND_LIBRARY_SUFFIXES=".a"
  3. Check your resulting executable with ldd to check for any dynamic libraries remaining.

I can’t guarantee the above will work though!

3 Likes

Hi,
thank you for all your tips!
I finally created .exe with static libraries and runned successfully on other linux computers,
so i think everything is ok.
Of course libraries are inside the package because of the dimensions (25MB vs 50kB).

Unfortunately problems came out running my executable on the computing centre (even with your last guide),
but at this point I think is not my problem; I will show this issue to the computing centre admininistrators.

Thank you again for helping me!
may the force be with you

Best Regards
Raffaele