I am running cmake and it is passing a flag to my linker that is unrecognized (-rdynamic), and it's causing an error.
I cannot figure out where it is getting this flag from, so I want to just filter it out.

I can specify -DCMAKE_LINKER=<linker>, so what I would like to do is set <linker> to a program that reads its command line arguments, filters out the bad one, and then passes the result back to the actual linker.

I have been using awk '{gsub("-rdynamic", "");print}', but I don't know to make the input stdin and the output ld.

  • 1
    Hello and welcome to this site! Can't you edit directly CMakeLists.txt and remove the offending flag? That said, if it's there, there should be a good reason... – Mr Shunz Oct 5 '18 at 15:52
  • Do you really mean "reads from stdin"? It sounds like you need a wrapper script that takes any number of command-line arguments, drops "-rdynamic" if it's one of them, and invokes the linker with the rest. – JigglyNaga Oct 5 '18 at 15:57
  • I don't think that "filtering out" the option is the correct thing to do. It would be better to make CMake detect the correct options to use. This is after all what CMake is supposed to do. – Kusalananda Oct 5 '18 at 15:58
  • I can edit CMakeLists.txt, but the problem is that I can't figure out where the flag is coming from. I grepped everything for the flag or similar keywords but there is nothing explicitly adding it. I am also pretty sure that -rdynamic is a flag intended for the compiler, the linker flag is --export-dynamic, so it may actually be a bug in the cmake files. – RJTK Oct 5 '18 at 16:15
  • @JigglyNaga this is an apt description of what I am trying to do. – RJTK Oct 5 '18 at 16:15

This bash script loops through its arguments, ignoring those matching the string "-rdynamic", and adding any others to an array. Once it runs out of arguments, it executes ld with the filtered list.


declare -a finalopts

for o in "$@"; do
    if [ "$o" = "-rdynamic" ] ; then
    #add all other options to the list

exec ld "${finalopts[@]}"
  • Awesome. This is exactly what I was trying to do. – RJTK Oct 5 '18 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.