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 7805721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:18:22+00:00 2026-06-02T02:18:22+00:00

If you are trying to understand dynamic linking, this question is likely to be

  • 0

If you are trying to understand dynamic linking, this question is likely to be of interest.

One of the answers to that question provides a wonderful example of creating and using a dynamic library. Based on it, I some simple files:

main.c:

extern void someFunction (int x);

int main (int argc, char** argv ) {
    someFunction(666);
}

mylibrary.c:

#include <stdio.h>

void someFunction (int x) {
    printf ("\nsomeFunction called with x=%d\n", x);
}

makefile:

main: mylibrary.c main.c
    gcc -c mylibrary.c
    gcc -dynamiclib -current_version 1.0 mylibrary.o -o libmylibrary.dylib
    gcc -c main.c
    gcc -v main.o ./libmylibrary.dylib -o main

clean:
    rm *.o
    rm main
    rm *.dylib

So far, everything works great. If I make and then enter ./main at the command prompt, I see the expected output:

someFunction called with x=666

Now, I want to mix things up a little. I’ve created a directory hidelib, which is a subdirectory of my main directory. And I’m adding one line to my makefile:

main: mylibrary.c main.c
    gcc -c mylibrary.c
    gcc -dynamiclib -current_version 1.0 mylibrary.o -o libmylibrary.dylib
    gcc -c main.c
    mv libmylibrary.dylib hidelib     # this is the new line

clean:
    rm *.o
    rm main
    rm hidelib/*.*

Now, I want to add another line to the makefile so it will find libmylibrary.dylib in the hidelib subdirectory. I want to be able to run ./main in the same way. How can I do that?

EDIT: Thanks for the response. Having lots of options is wonderful, but a beginner just wants one concrete option that works. Here is what I am trying for the last line, but clearly I don’t understand something. The makefile executes without errors, but at runtime it says “library not found.”

    gcc main.o -rpath,'$$ORIGIN/hidelib' -lmylibrary -o main
  • 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-02T02:18:24+00:00Added an answer on June 2, 2026 at 2:18 am

    One concrete option that works would be to set the install_name flag when linking the .dylib.

    gcc -dynamiclib -install_name '$(CURDIR)/hidelib/libmylibrary.dylib' -current_version 1.0 mylibrary.o -o libmylibrary.dylib
    

    Then you can just link to the library normally:

    gcc main.o -L '$(CURDIR)/hidelib' -lmylibrary -o main
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the process of dynamic linking, in detail. For the
I am trying to understand dynamic linq and expression trees. Very basically trying to
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
Just trying to understand that - I have never used it before. How is
My first post on this site with huge hope:: I am trying to understand
I'm having some trouble trying to understand how to create a dynamic choice field
I'm using CakePHP 2.0 and I'm trying to understand how I can echo dynamic
This is probably a silly question, but I am trying to stuff an anonymous
I'm trying to pass context into a dynamic expression that I evaluate every iteration

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.