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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:41:16+00:00 2026-05-16T15:41:16+00:00

I have a set of cpp files that I want to compile directly into

  • 0

I have a set of cpp files that I want to compile directly into a binary and also to compile into a shared library.

I have

bin_PROGRAMS=mybin
lib_LTLIBRARIES=libmylib.la

COMMON_SOURCES=f1.cpp f2.cpp f3.cpp

mybin_SOURCES=main.cpp $(COMMON_SOURCES)
libmylib_la_SOURCES=$(COMMON_SOURCES)

When I run this the cpp files are compiled twice, once with libtool and once without and sometimes libtool/automake complains

Makefile.am: object `f1.$(OBJEXT)' created both with libtool and without`

I tried putting COMMON_SOURCES into a .a file but then libtool complains when I link a .a with a .la (saying its not portable).

What I need is something like

bin_LTPROGRAMS=mybin

but that doesnt exist

edit: clarification – I am using automake/autoconf. What I have shown above is the meat of my automake Makefile.am

  • 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-16T15:41:17+00:00Added an answer on May 16, 2026 at 3:41 pm

    The issue is that the common sources need to be compiled differently when they are being made into a shared object than when they are being made into a static archive; in the case of the former, for example, g++ needs to be passed the -fPIC flag.

    What I suggest is using two build directories.

    Assuming this source hierarchy:

    ./src/Makefile.am
    ./src/f1.cpp
    ./src/f2.cpp
    ./src/f3.cpp
    ./src/main.cpp
    ./configure.ac
    ./Makefile.am
    

    you would use something like this in ./src/Makefile.am:

    bin_PROGRAMS = mybin
    lib_LTLIBRARIES = libmylib.la
    
    mybin_SOURCES = main.cpp
    mybin_LDADD = libmylib.la
    
    libmylib_la_SOURCES = f1.cpp f2.cpp f3.cpp
    

    Then you create directories Release and ReleaseDisableShared in ./. In directory ./Release you run:

    ../configure && make
    

    and in ./ReleaseDisableShared you run:

    ../configure --disable-shared && make
    

    After building in each build directory, you use the mybin at ./ReleaseDisableShared/src/mybin and the libmylib.so at ./Release/src/libmylib.so.

    See also:

    • Position-independent code
    • build .o files to specific directory using automake
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class of students which I store into set in my cpp
Help me with makefile, please. I want `make' to recompile only files that have
I have set my default RVM to ruby 1.9.2 . I want to remove
I have the following code that cannot be compiled: #include <iostream> #include <set> #include
I have two programs, recvfile.py and sendfile.cpp. They work except that I end up
I have a 3rd party library that's written in C. It exports all of
I've figured out how to set VC++ to compile code into a .lib file
Help me Stackoverflow! So, I feel that I have set up my environment correctly
I have set up the Nginx +unicorn for my rails application, it is working
I have set up an admin panel for charter, yacht and other vehicles reservation

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.