You probably want to replace your use of link_directories
with find_library
and target_link_libraries
.
link_directories
only provides paths which the linker can search for dependencies - it doesn't actually specify what those dependencies are. Furthermore, from the docs:
Note that this command is rarely necessary. Library locations returned by
find_package()
andfind_library()
are absolute paths. Pass these absolute library file paths directly to thetarget_link_libraries()
command. CMake will ensure the linker finds them.
I'm not familiar with Xerces, but assuming it has only 1 library called "libxerces-c.a", you should probably have something like:
find_library(XercesLibrary NAMES xerces-c PATHS /usr/local/lib)
if(NOT XercesLibrary)
message(FATAL_ERROR "Failed to find the Xerces library.")
endif()
...
target_link_libraries(MyExe ${XercesLibrary})
You may need to significantly extend this find_library
process; more PATHS
than just /usr/local/lib
could be given; you may need to find more than 1 library (e.g. a Debug version on Windows?), etc. If the library has different names on different operating systems, you may need to provide more NAME
options (remember CMake may adjust the search name - see CMAKE_FIND_LIBRARY_PREFIXES
and CMAKE_FIND_LIBRARY_SUFFIXES
).
Also, a more helpful error message can be invaluable if the find attempt fails. You could suggest to set a variable (e.g. XERCES_LIB_DIR
) indicating the location of the Xerces library, and this could be added to the list of PATHS
in the find_library
call.