Skip to content

Fix cmake build: use target_link_libraries instead of LINK_FLAGS#1

Open
rljacobuc wants to merge 2 commits intomainfrom
fix/cmake-link-order
Open

Fix cmake build: use target_link_libraries instead of LINK_FLAGS#1
rljacobuc wants to merge 2 commits intomainfrom
fix/cmake-link-order

Conversation

@rljacobuc
Copy link
Owner

Problem

The cmake build failed with hundreds of undefined references to wxWidgets and NetCDF symbols during linking. The root cause is that LINK_FLAGS in set_target_properties places 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_FLAGS with the proper cmake mechanisms:

  • target_link_options for the rpath linker flag
  • target_link_libraries for wxWidgets and NetCDF library flags

This ensures libraries appear after the object files in the final linker invocation, as required by GNU ld.

The build.sh script (which invokes g++ directly with libraries after sources) already worked correctly and continues to work.

This PR was generated with Oz.

rljacobuc and others added 2 commits March 5, 2026 22:55
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 <[email protected]>
- 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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant