Fix cmake build: use target_link_libraries instead of LINK_FLAGS#1
Open
Fix cmake build: use target_link_libraries instead of LINK_FLAGS#1
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 <[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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.