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

  • Home
  • SEARCH
  • 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 8753041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:21:03+00:00 2026-06-13T13:21:03+00:00

I have the following C files: Base.h: void g(); void h(); Base.c: #include <stdio.h>

  • 0

I have the following C files:

Base.h:

void g();
void h();

Base.c:

#include <stdio.h>
#include <Base.h>

void g() {
  printf("This is lib g\n");
  h();
}

void h() {
  printf("This is lib h\n");
}

A.c:

#include <stdio.h>
#include <Base.h>

void h() {
  printf("This is A h\n");
}

void main() {
  g();
  h();
}

I compile and link as follows:

$ gcc -c -fPIC -o Base.o -I. Base.c
$ gcc -shared -o libBase.so Base.o
$ gcc -c -o A.o A.c
$ gcc -o A A.o -lBase -L.

Now I run the program

$ LD_LIBRARY_PATH=. ./A

and obtain:

This is lib g
This is A h
This is A h

That means, the call to h in libBase is resolved by h of A.o. This is not what I expected. I either expected the dynamic linker to resolve the call to h in libBase with h in libBase or an error message in the fourth gcc call.

If I rename h in A.c to h1

#include <stdio.h>
#include <Base.h>

void h1() {
  printf("This is A h1\n");
}

void main() {
  g();
  h1();
}

I obtain

This is lib g
This is lib h
This is A h1

So, in this case h is resolved as I expected.

What do I have to do to either obtain an error message or to have the call in g resolved to h in libBase?

  • 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-06-13T13:21:04+00:00Added an answer on June 13, 2026 at 1:21 pm

    This is not what I expected.

    Your expectation is wrong. This is the way shared libraries on most UNIX systems work: the loader simply goes down the list of loaded libraries, and tries to find the given symbol. The first library to define the symbol “wins”.

    This behavior has many advantages. One example: you can LD_PRELOAD libtcmalloc.so, and suddenly all malloc and free calls resolve to tcmalloc.

    On ELF systems, you can modify this behavior with the -Bsymbolic linker flag (use -Wl,-Bsymbolic when passing it through GCC). Beware: -Bsymbolic goes “against the system”, and you may get many unexpected and undesirable side-effects as a result.

    See also this answer.

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

Sidebar

Related Questions

If I have the following files, I get this error (c2593 in VC9). If
I have a project in which i have the following files main.wxs - directory
I have the following js files in my code for EditInPlace... <?php echo $javascript->link('jquery.js');?>
I have the following three files in the same directory: citysim.cpp #include utils.h using
I have this abstract base class ODESolver: //ODESolver.h #ifndef ODESolver_H #define ODESolver_H #include doublependulum.h
I have a library, libfoo which is made of the following files: base.hpp #ifndef
#include <stdio.h> #include <math.h> long int input; long int base, exponent; void convert(long int
I have following script that executes all the .reg files in the current directory.
I have the two following text files: First one: chr10 1000 1001 DEL 2.4807
I have a directory named 'backup' its contains following files (with some specific details)

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.