0

I am new to C++ and XML and i am using cmake to compile some GEANT4 codes. after the command "make, there seems to a problem with linking the CXX executable

I am running it on Mac OSX10.14.6, geant4 10.5, The C/CXX compiler identification is AppleClang 10.0.1.10010046

This is the code which links the XML part.

XMLConfiguration* Cfg = new XMLConfiguration("mammact");
Cfg->readFile("mammact.xml");

When i remove all the XML part related to the code from the main.cc,include and src, i can run it. But when i include the XML part, the following errors appear;

[100%] Linking CXX executable mammactcone

Undefined symbols for architecture x86_64:
"xercesc_3_2::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_2::PanicHandler*, xercesc_3_2::MemoryManager*)", referenced from:

XMLConfiguration::XMLConfiguration(char const*) in XMLConfiguration.cc.o
.
.
.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Since i am new to programming, I'm learning things as i'm coding, so i kindly request you to provide solutions/suggestion a bit clear than how you would do to a fellow prorgrammer :)

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.12)

project(SimulationMAMA)
find_package(MPI REQUIRED)

#-------------------------------------------------------------------------
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
#
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
  find_package(Geant4 REQUIRED ui_all vis_all)
else()
  find_package(Geant4 REQUIRED)
endif()

find_package(G4mpi REQUIRED)

#-------------------------------------------------------------------------
# Setup Geant4 include directories and compile definitions
# Setup include directory for this project
#
include(${Geant4_USE_FILE})
include_directories(${PROJECT_SOURCE_DIR}/include)


#-------------------------------------------------------------------------
# Locate sources and headers for this project
# NB: headers are included so they will show up in IDEs
#
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include
                              ${Geant4_INCLUDE_DIR}
                              ${G4mpi_INCLUDE_DIR}
                              ${Xerces_INCLUDE_DIR})

#-------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#
add_executable(mammactcone mammactcone.cc ${sources} ${headers})
target_link_libraries(mammactcone ${Geant4_LIBRARIES})


#-------------------------------------------------------------------------
# Copy scripts to the build directory of XRFCT.
#
set(MY_SCRIPTS
    mammact.xml init_vis.mac vis.mac run1.mac
  )

foreach(_script ${MY_SCRIPTS})
  configure_file(
    ${PROJECT_SOURCE_DIR}/${_script}
    ${PROJECT_BINARY_DIR}/${_script}
    COPYONLY
    )
endforeach()

#set(CMAKE_INCLUDE_CURRENT_DIR ON)
#set(CMAKE_AUTOMOC ON)

#find_package(Qt5Core)

#add_executable(${PROJECT_NAME} "mammactcone.cc")

#target_link_libraries(${PROJECT_NAME} Qt5::Core)

#-------------------------------------------------------------------------
# For internal Geant4 use - but has no effect if you build this
# example standalone
#
#add_custom_target(mammactcone DEPENDS mammactcone)
#-------------------------------------------------------------------------
# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX

install(TARGETS mammactcone DESTINATION bin)
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – walnut Aug 28 '19 at 12:21
  • You will need to instruct CMake to link your `mammactcone` target against xerxes. What does your CMakeLists.txt look like? – Botje Aug 28 '19 at 12:33
  • @Botje This is the part of the CMakeList.txt where i introduced Xerces file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc) file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${Geant4_INCLUDE_DIR} ${Xerces_INCLUDE_DIR}) # Add the executable, and link it to the Geant4 libraries add_executable(mammactcone mammactcone.cc ${sources} ${headers}) target_link_libraries(mammactcone ${Geant4_LIBRARIES}) # Should also add the ${Xerces_LIBRARIES} in this line? – Hungerdemon95 Aug 28 '19 at 15:20
  • Please, add that code to the **question post** itself. As you can see, the comments are badly suited for multi-line code. Also, it is a **requirement** of the Stack Overflow to have the code in the question post, not in the comments. – Tsyvarev Aug 28 '19 at 21:34
  • 1
    Yes, you should, assuming that this variable exists. – Botje Aug 29 '19 at 07:21
  • @Tsyvarev Thank you, I thought there was a word limit for codes or somethings. CMakeLists.txt updated in the question – Hungerdemon95 Aug 29 '19 at 22:12
  • As @Botje says, you need to link with `Xerces` library. But I don't see `find_package()` call in your code for Xerces, so the `Xerces_*` variables are unlikely set. – Tsyvarev Aug 29 '19 at 22:39
  • @Botje Thank you very much. I made a mistake of not linking that library properly. – Hungerdemon95 Aug 30 '19 at 12:27

1 Answers1

0

After Xerces libraries were linked properly, the issue seemed to be resolved.

target_link_libraries(mammactcone ${Geant4_LIBRARIES} ${MPI_LIBRARIES} ${XercesC_LIBRARIES})