protocolbuffers / protocolbuffers/protobuf

Define cmake target as PIC if BUILD_SHARED_LIBS

Open
#10,828 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

c++ cmake help wanted
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

What language does this apply to?
Generated C/C++ code and the specific cmake target.

Describe the problem you are trying to solve.
When I link to the generated cmake target and set the BUILD_SHARED_LIBS variable globally I get linker errors because the protobuf created code is not relocateable.

Describe the solution you'd like
In my user code I use something like the following:

# ensure relocateable code
if (BUILD_SHARED_LIBS)
  set_property(TARGET ${PROTO_TARGET} PROPERTY POSITION_INDEPENDENT_CODE TRUE)
endif ()

But this could be integrated in the cmake part of this project directly.

Describe alternatives you've considered
It's not a big problem. But may be helpful for people that do not have that much cmake experience.

Additional context
Nothing.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the project's CMake configuration where the generated target is defined, and inspect how BUILD_SHARED_LIBS is handled. Confirm the target's POSITION_INDEPENDENT_CODE behavior, then build a shared-library configuration and verify that generated C/C++ code links without relocation errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.