0

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.

xvan
  • 4,554
  • 1
  • 22
  • 37
  • 2
    Maybe just an idea, but instead of compiling the generated files, could you instead wrap them in a namespace? i.e. `namespace autogen1 {#include "autogenerated1.cpp"}` http://stackoverflow.com/questions/232693/including-one-c-source-file-in-another – Lanting Mar 04 '16 at 14:28
  • Awesome workaround! It needs to be done inside the same namespace of the wrapper class to avoid conflicts with the wapped headers. - It's also a cross platform solution. Thanks! – xvan Mar 04 '16 at 16:30

1 Answers1

1

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?

Lanting
  • 3,060
  • 12
  • 28