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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:13:22+00:00 2026-05-16T04:13:22+00:00

I was reading about static and dynamic libraries. To explore more I created three

  • 0

I was reading about static and dynamic libraries. To explore more I created three files 2 .cpp files and 1 .h file

demo.h

 class demo
{

    int a;

    public:

    demo();
    demo(const demo&);
    demo& operator=(const demo&);
   ~demo();
};

demo.cpp

#include "demo.h"
#include <iostream>
demo::demo():a()
{
    std::cout<<"\nInside default constructor\n";
}

demo::demo(const demo&k)
{

    this->a=k.a;
    std::cout<<"\nInside copy constructor\n";
}

demo& demo::operator=(const demo&k)
{
    this->a=k.a;
    std::cout<<"\nInside copy assignment operator\n";
    return *this;
}

demo::~demo()
{
    std::cout<<"\nInside destructor\n";
}

main.cpp

#include "demo.h"

int main()
{

   demo d;
   demo d1=d;
   demo d2;
   d2=d;
}

Now I created two object files :demo.o and main.o using g++ -c demo.cpp and g++ -c main.cpp and then created a static library using ar cr demo.a demo.o main.o

I also created a dynamic library using g++ -shared demo.o main.o -o demo.dll

Now when I use my static library(g++ demo.a -o demo) to create an executable everything goes fine. But when I use my dynamic library to create an executable I get an error Undefined reference to main
I have used the following command to create an executable g++ demo.dll -o demo.

When I use g++ main.cpp -o demo demo.dll everything goes fine, why?

Where am I wrong?

  • 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-16T04:13:22+00:00Added an answer on May 16, 2026 at 4:13 am

    When compiling code for a .so (or .dll as you call it) that code needs to be position independent. Man gcc:

       -shared
           Produce a shared object which can then be linked with other objects
           to form an executable.  Not all systems support this option.  For
           predictable results, you must also specify the same set of options
           that were used to generate code (-fpic, -fPIC, or model suboptions)
           when you specify this option.
    
       ...
    
       -fpic
           Generate position-independent code (PIC) suitable for use in a
           shared library, if supported for the target machine.  Such code
           accesses all constant addresses through a global offset table
           (GOT).  The dynamic loader resolves the GOT entries when the
           program starts (the dynamic loader is not part of GCC; it is part
           of the operating system).  If the GOT size for the linked
           executable exceeds a machine-specific maximum size, you get an
           error message from the linker indicating that -fpic does not work;
           in that case, recompile with -fPIC instead.  (These maximums are 8k
           on the SPARC and 32k on the m68k and RS/6000.  The 386 has no such
           limit.)
    
           Position-independent code requires special support, and therefore
           works only on certain machines.  For the 386, GCC supports PIC for
           System V but not for the Sun 386i.  Code generated for the IBM
           RS/6000 is always position-independent.
    
           When this flag is set, the macros "__pic__" and "__PIC__" are
           defined to 1.
    

    In other words:

    g++ -o main.o -c -fpic main.cpp
    g++ -o demo.o -c -fpic demo.cpp
    g++ -o demo.dll -shared main.o demo.o
    g++ -o demo demo.dll
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading about the System.Diagnostics.Contracts.Contract static class that has been influenced by the awesomeness
While reading about constant interface antipattern, i found final constant class with no instances
I'm reading about constructors, When an object is instantiated for a class, c'tors (if
I've been reading articles about speeding up websites by serving static content from a
I've been reading this section about static methods and about passing arrays by call-by-reference.
I was reading about the fact that LLVM v3 uses a static analysis of
I was reading about making code more portable by using fixed-width integers. I found
I am reading about 6000 text-files into memory with the following code in a
I've been reading online about the pros and cons of using static methods for
The program is basically about reading from a text file, storing the current data

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.