Problem 1197

Summary: Windows DLLs are not built under CMake
Product: Geant4 Reporter: Ben Morgan <Ben.Morgan>
Component: cmakeAssignee: Ben Morgan <Ben.Morgan>
Status: CLOSED FIXED    
Severity: major    
Priority: P2    
Version: 9.4   
Hardware: PC   
OS: Windows   

Description Ben Morgan 2011-04-21 15:39:27 CEST
The current CMake build does not create DLLs on Windows (Visual Studio). 

The key tasks are:

a) Geant4 code requires generation of a .def file to list the symbols to be exported by the DLL as it does not in the main instrument with __declspec.
b) *Some* symbols, those for allocators, are exported using __declspec and so require the correct compiler definitions to be set.

The generation of the .def file can be done through the code supplied by Pere Mato.
Comment 1 Ben Morgan 2011-05-19 16:45:30 CEST
On item b), there are two sets of definitions to be handled:

1) G4LIB_BUILD_DLL : this must be used globally if we want to build DLLs.

It is directly used by the source files

geant4/source/global/management/include/G4Types.hh
geant4/source/geometry/volumes/include/G4NavigationHistory.hh
geant4/source/geometry/volumes/src/G4NavigationHistory.cc

and consequently any other files that #include those. G4Types.hh is the key file because this will define the needed export/import symbols for us if:

i) WIN32 is defined, and
ii) G4LIB_BUILD_DLL is defined.

However, my understanding is that __declspec should only be used for DLLs. Because we offer the option to build both static and dynamic libraries, this should only be used for DLLs. That means adding a target level COMPILE_DEFINITIONS vis set_target_properties, so this can go in Geant4MacroLibraryTargets.cmake.

2) XXX_EXPORTS : these are generally defined at the category level and used to correctly define externs with import/export as needed. They are always needed, on all platforms and independent of whether DLLs are built or not, when Geant4 itself is built. Because they have many different names, but are at least category based, it's easiest to set them as an add_definitions command in the category level CMakeLists.txt.
Comment 2 Ben Morgan 2011-05-19 18:54:35 CEST
XXX_EXPORTS added to:

global (G4IOS_ALLOC_EXPORT), tag: global-V09-04-08
geometry (G4GEOM_ALLOC_EXPORT), tag: geometry-V09-04-00
Comment 3 Ben Morgan 2011-05-20 15:37:18 CEST
All XXX_EXPORTS now added to trunk, plus tagged as:

global (G4IOS_ALLOC_EXPORT), tag: global-V09-04-08
geometry (G4GEOM_ALLOC_EXPORT), tag: geometry-V09-04-00
particles (G4PARTICLES_ALLOC_EXPORT), tag: particles-V09-04-05
track (G4TRACK_ALLOC_EXPORT), tag: track-V09-04-03
digits_hits (G4DIGI_ALLOC_EXPORT), tag: digits_hits-V09-04-05
tracking (G4TRACKING_ALLOC_EXPORT), tag: tracking-V09-04-01
event (G4EVENT_ALLOC_EXPORT), tag: event-V09-04-01
run (G4RUN_ALLOC_EXPORT), tag: run-V09-04-06
raytracer (G4VIS_ALLOC_EXPORT), tag: raytracer-V09-04-02
Comment 4 Ben Morgan 2011-06-15 11:23:16 CEST
CMake category updated with:

1) New genwindef app (courtesy of Pere Mato Vila) to generate exported symbol definition file from an archive library.

2) New functionality in Geant4MacroLibraryTargets.cmake to use genwindef plus an existing or temporary archive library to generate the .def export file and build a DLL from this plus a dummy cpp file.

Tested on Windows 7 with Visual Studio 2010 Express.

Committed to SVN with tag:

cmake-V09-04-01

Tag proposed on database, will mark as RESOLVED FIXED once tag is accepted.