1

I want use two version of static librares in my project. And I found an answer from Restricting symbols in a Linux static library.

But I don't know how to use cmake to run following commands:

ld -r obj1.o obj2.o ... objn.o -o static1.o
objcopy --localize-hidden static1.o static2.o
ar -rcs mylib.a static2.o
Kevin
  • 16,549
  • 8
  • 60
  • 74
xuhuleon
  • 23
  • 4
  • CMake has no builtin support for these commands. You need [`add_custom_command`](https://cmake.org/cmake/help/v3.13/command/add_custom_command.html) – Mike Kinghan May 07 '19 at 14:32

2 Answers2

2

Use

find_program(CMAKE_OBJCOPY objcopy)

or

find_program(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)

where TOOLCHAIN_PREFIX is path to your custom toolchain (for example, if you build your solution for ARM, MIPS e.t.c.). See example

Then you may use command like this (use execute_process):

    execute_process(COMMAND
      ${CMAKE_OBJCOPY} --localize-hidden static1.o static2.o
      RESULT_VARIABLE objcopy_result
      ERROR_QUIET
      OUTPUT_VARIABLE objcopy_out)

or use add_custom_command

0

You could use execute_process to call the command, like this:

execute_process(COMMAND "ld" "-r" "obj1.o" "obj2.o" ... WORKING_DIRECTORY "...") 

or use add_custom_command like here

Kevin
  • 16,549
  • 8
  • 60
  • 74