Search code examples
c++gcccmakevisibilitystatic-linking

cmake and partial linking to solve symbol collisions


I have two sources with name collisions (autogenerated1.cpp and autogenerated2.cpp).

I don't want to modify them as they're generated by a 3rd party tool, so I made wrappers to hide the implementations.

I'd like to do something like this on CMAKE:

g++ -fvisibility=hidden -c autogenerated1.cpp

g++ -fvisibility=hidden -c autogenerated2.cpp

g++ -c wrapper1.cpp

g++ -c wrapper2.cpp

ld -Ur autogenerated1.o wrapper1.o -o partial1.o

ld -Ur autogenerated2.o wrapper2.o -o partial2.o

objcopy --localize-hidden partial1.o

objcopy --localize-hidden partial2.o

How could I achieve something similar with CMAKE?

The TARGET_OBJECT generator doesn't work on custom commands or custom targets.

And I wasn't able to produce a working static library by applying "objcopy --localize-hidden" to the a target's output shared library without a relocatable intermediate object.

I'm open to other solutions to fix the symbol collision.


Solution

  • Nice to see that it works for your case. Now as an answer instead of a reply:

    Maybe just an idea, but instead of compiling the generated files, could you instead wrap them in a namespace? e.g:

    namespace autogen1 {
    #include "autogenerated1.cpp"
    }
    

    You'd obviously need to exclude the autogenerated files from compiling (as those are now pulled in through the include), but you now have access to all members of both autogenerated files, simply by prefixing the calls with the appropriate namespaces.

    It looks a bit hackish (but not as bad as your own solution ;-) )

    Including one C source file in another?