Skip to content

Rely on CMAKE_POSITION_INDEPENDENT_CODE #2384

@lemire

Description

@lemire

Position-Independent Code (PIC) is a way of compiling code so that it can be loaded and executed at any memory address without needing modifications. Instead of saying, “Call the function at memory address 0x1234,” PIC code says, “Call the function that’s 500 bytes from my current location.” This makes the code flexible—it can run correctly no matter where in memory it’s loaded.

Shared libraries (like .so files on Linux or .dll files on Windows) need to be PIC to work correctly.

When you create a shared library in CMake, like this:

add_library(my_shared_lib SHARED source.c)

CMake automatically adds the -fPIC flag (or equivalent, depending on the compiler) to the compiler’s command line. This flag tells the compiler to generate PIC.

But why would we need PIC for static libraries?

If the static library is used in a dynamic library and it wasn’t compiled with PIC, then the build might fail.

But you can tell CMake to default on PIC by setting CMAKE_POSITION_INDEPENDENT_CODE.

So if you grab library online, you can choose to get a PIC by default everywhere by setting a flag. Easy!

So I don't think we need to force PIC in simdjson.

Ref: #2383

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions