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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:49:37+00:00 2026-06-05T20:49:37+00:00

I am starting a project which has several applications. These applications share some common

  • 0

I am starting a project which has several applications. These applications share some common functionality in terms of source files. Each application should have its own make file but be able to link in these shared source files from a common location.

To illustrate this, here’s my directory layout:

project/
    lib/
        shared1/
            shared1.c
            shared1.h
        shared2/
            shared2.c
            shared2.h
    app1/
        app1.c
        app1.h
        makefile
    app2/
        app2.c
        app2.h
        makefile

Currently one of the makefiles looks a bit like this:

CC=gcc
XXXCFLAGS=-Wall -Wextra -pedantic -I../../lib/shared1
CFLAGS=-I. $(XXXCFLAGS)
DEPS = app2.h ../../lib/shared1/shared1.h
OBJ = ${DEPS:.h=.o}
LIBS=-lpthread

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

app2: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
  1. Is this roughly the right way to go about managing shared source, or is there a better way. Should they be separately compiled libraries? If so, how?

  2. Are relative paths like this ok?

  3. Should I be using autotools for something like this? After looking at examples I couldn’t see how to link shared code or a library.

Any guidance is most appreciated.

  • 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-05T20:49:40+00:00Added an answer on June 5, 2026 at 8:49 pm

    This is a reasonable way to lay out your source tree, although you might consider separating source files and header files. If you want to build a library (if many apps use, say, shared1) you can write a rule for it, and put the rule in a makefile in shared1/. There are several ways to use such a rule, but don’t worry about that until you’re sure you want a library.

    Paths are a necessary evil. They lock the makefile to a particular directory layout, but you can’t entirely do without them. What you can do is keep them corralled:

    SHARED1LIB = ../../lib/shared1
    XXXCFLAGS=-Wall -Wextra -pedantic -I$(SHARED1DIR)
    DEPS = app2.h $(SHARED1DIR)/shared1.h
    

    Your use of dependencies is a little peculiar. For one thing, it’s better to derive the list of objects from the list of sources, since you might have a source without a header or a header without a source:

    SRCS = app2.c $(SHARED1DIR)/shared1.c
    OBJ = ${SRCS:.c=.o}
    

    Second, you make every object file depend on all headers, which is probably overkill. Suppose we can assume foo.o will depend on foo.h, and also that app2.o depends on shared1.h; then we can write a much better rule:

    %.o: %.c %.h
        $(CC) -c -o $@ $< $(CFLAGS)
    
    app2.o: $(SHARED1DIR)/shared1.h
    

    If you have a lot of objects and this approach is too cumbersome, there is a very sophisticated method of handling these lists automatically, but it’s an advanced technique you’d probably better not try just yet.

    Finally, if you find that all of your app makefiles look much alike, you can take all of the common stuff and put it in a makefile in project/

    SHARED1LIB = ../../lib/shared1
    SHARED2LIB = ../../lib/shared2
    
    CC=gcc
    XXXCFLAGS=-Wall -Wextra -pedantic
    CFLAGS=-I. $(XXXCFLAGS)
    OBJ = ${SRCS:.c=.o}
    LIBS=-lpthread
    
    %.o: %.c %.h
        $(CC) -c -o $@ $< $(CFLAGS)
    
    app%: $(OBJ)
        $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    

    Then the app makefiles can look like this:

    SRCS = app2.c $(SHARED1DIR)/shared1.c
    
    app2:
    include ../Makefile
    
    XXXCFLAGS += -I$(SHARED1DIR)
    
    app2.o: $(SHARED1DIR)/shared1.h
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting with a blank project, when I load a SWF which has a dependence
All, I'm starting a new ASP.NET MVC project which requires some content management capabilities.
I am starting work on a project which has multiple websites for a client.
I'm starting on a new scientific project which has a lot of data (millions
We're starting a new project that has some messaging and queueing requirements that are
I'm starting on a new project that has some hierarchical data and I'm looking
I will be starting a project which requires communication between distributed nodes(the project is
I am starting with a test automation project which is based on keyword driven
I am shortly starting a project, which requires messages to be held in a
I'm starting a new project, for this project, I need a great IDE, which

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.