Fix cmake build: use target_link_libraries instead of LINK_FLAGS - #1
Fix cmake build: use target_link_libraries instead of LINK_FLAGS#1rljacobuc wants to merge 2 commits into
Conversation
LINK_FLAGS places library flags before object files in the linker command, causing undefined reference errors with GNU ld which requires libraries to appear after the object files that use them. Replace LINK_FLAGS with target_link_options (for rpath) and target_link_libraries (for wxWidgets and NetCDF libraries) so that the link order is correct. Co-Authored-By: Oz <oz-agent@warp.dev>
- Use BUILD_RPATH/INSTALL_RPATH properties instead of raw -Wl,-rpath flag - Use target_link_directories instead of -L flag in separate_arguments - Split link items into libraries (target_link_libraries) and flags (target_link_options) - Use NATIVE_COMMAND instead of UNIX_COMMAND in separate_arguments Co-Authored-By: Oz <oz-agent@warp.dev>
| # Link libraries after object files (required for GNU ld) | ||
| separate_arguments(WX_LINK_ITEMS NATIVE_COMMAND "${WX_LINK_FLAGS}") | ||
| separate_arguments(NC_LINK_ITEMS NATIVE_COMMAND "-lnetcdf ${NC_LINK_FLAGS}") | ||
| target_link_directories(ncvis PRIVATE ${NC_LIB_DIR}) |
There was a problem hiding this comment.
🟡 Build configuration fails on older CMake versions the project still claims to support
The build script now uses two configuration commands (target_link_directories/target_link_options at src/CMakeLists.txt:44 and src/CMakeLists.txt:72) that only exist in newer CMake, while the project still declares support for a much older minimum version, so configuring the build stops with an error for anyone on an older CMake.
Impact: Users with an older but supposedly supported CMake can no longer configure or build the program at all.
Version requirements of the newly introduced CMake commands
The top-level CMakeLists.txt:1 declares cmake_minimum_required(VERSION 3.1). The new code requires:
target_link_directories— CMake 3.13+target_link_options— CMake 3.13+separate_arguments(... NATIVE_COMMAND ...)— CMake 3.9+BUILD_RPATHtarget property — CMake 3.8+
With CMake < 3.13 the configure step aborts with "Unknown CMake command "target_link_directories"". The minimum required version should be bumped to at least 3.13 (and INSTALL.md build instructions updated if it mentions a version).
Prompt for agents
src/CMakeLists.txt now uses target_link_directories and target_link_options (CMake 3.13+), separate_arguments(NATIVE_COMMAND) (3.9+) and the BUILD_RPATH property (3.8+), but the top-level CMakeLists.txt still says cmake_minimum_required(VERSION 3.1). Configuration will fail with 'Unknown CMake command' on older CMake. Raise the declared minimum required version to match what the new commands need, and check whether INSTALL.md documents a CMake version that should be updated too.
Was this helpful? React with 👍 or 👎 to provide feedback.
Problem
The cmake build failed with hundreds of undefined references to wxWidgets and NetCDF symbols during linking. The root cause is that
LINK_FLAGSinset_target_propertiesplaces library flags before the object files in the linker command. GNU ld processes flags left-to-right, so libraries listed before the object files that reference them have no unresolved symbols to satisfy and their symbols are discarded, causing the undefined reference errors.Fix
Replace
LINK_FLAGSwith the proper cmake mechanisms:target_link_optionsfor the rpath linker flagtarget_link_librariesfor wxWidgets and NetCDF library flagsThis ensures libraries appear after the object files in the final linker invocation, as required by GNU ld.
The
build.shscript (which invokesg++directly with libraries after sources) already worked correctly and continues to work.This PR was generated with Oz.