Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:18:26+00:00 2026-05-25T12:18:26+00:00

I made a piece of code which consists in a dynamic library ( lib.c

  • 0

I made a piece of code which consists in a dynamic library (lib.c), and a main executable (main.c).
In both files I define a global variable named: int global.
Not very smart but it’s not the question.

When I compile the dynamic library the -fPIC option seems mandatory:

gcc lib.c -fPIC -shared -o lib.so

otherwise I get:

/usr/bin/ld: /tmp/ccpUvIPj.o: relocation R_X86_64_32 against '.rodata' can not be used when making a shared object; recompile with -fPIC

When I compile the executable it is not.

gcc main.c -fPIC -ldl
gcc main.c -ldl

Both work, but have different behaviours I can not explain, could you ? :

with -fPIC, global in main.c and global in lib.c are the same variables:

global main: 23 (0x601050)
global lib: 23 (0x601050)

without -fPIC, global in lib.c is not correlated to global in main.c:

global main: 23 (0x601048)
global lib: 0 (0x7f7742e64028)

Here is the source :

lib.c

#include <stdio.h>
#include <stdlib.h>

int global;

int f_one() {

    printf("global lib: %d (%p)\n", global, &global);

    return EXIT_SUCCESS;
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

void * handle;
int global;

int main() {

    int q = 7;

    int (* f_one_p)(int a) = NULL;

    global = 23;

    handle = dlopen("./lib.so", RTLD_NOW);

    if (handle == 0) {
        return EXIT_FAILURE;
    }

    f_one_p = dlsym(handle, "f_one");

    printf("global main: %d (%p)\n", global, &global);

    f_one_p(q);

    return EXIT_SUCCESS;

}

gcc –version: gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

uname -a: Linux xxx 2.6.38-11-generic #48-Ubuntu SMP Fri Jul 29 19:02:55 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

edit: code tested under SUN/sparc and x86/Linux architectures with the same kind of unexpected shared global variables (with -fPIC).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:18:27+00:00Added an answer on May 25, 2026 at 12:18 pm

    When you compile with -fPIC the object in question will determine the address of global symbols using the Global Offset Table. What happens though when part of the code is -fPIC and part isn’t is that one of your int globals will be using this table to determine the address whilst the other part isn’t.

    If you had two shared object linked with -fPIC, but your main program not then you would still have two addresses for int global, one using the global offset table and one which was just local to the non-PIC code.

    There’s a really great discussion on PIC vs pic vs non PIC if you want to read further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have time-critical piece of code in my app. I made the thread which
I'm writing a piece of open-source software which will be easily embeddable (both static/dynamic
I made this piece of php code which grabs a RSS feed, but I'm
I have made this piece of code that makes a roll hover effect on
Hi I have a generator which made some automated code (creating all the get,
I have made some c code for a program, which does some psycho-acoustics on
I made a certain piece of functionality which works perfectly on Chrome, but not
I have a piece of code that may or may not fail, which I
I made a code that translate strings to match each word from the array
I have been trying to optimize some code which handles raw pixel data. Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.