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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:47:10+00:00 2026-06-13T20:47:10+00:00

I have some source .cpp files in the same directory, and I want to

  • 0

I have some source .cpp files in the same directory, and I want to compile them as object file.
Some of them needs “extra” libraries (same libs for all files) to be linked with and some of them don’t, I’d like to write general rules to match the right files, without writing specific rule for each file with extra flags.
More in detail I want to refactor my Makefile so that I don’t have to specify different rules for the source files SourceFileOpenCVNeeded.cpp OCVAlsoHere.cpp TheSameForMe.cpp (now they’re few, but they could be many more).

Here is what my makefile actually looks like:

CPP_FILES := $(wildcard src/*.cpp)
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))
MAIN_SRC := main.cpp
OCV_LIBS := `pkg-config opencv --libs`
OCV_PATH := `pkg-config opencv --cflags`
MY_LIB := launcher
LD_FLAGS := $(MAIN_SRC) -L. -l$(MY_LIB) $(OCV_LIBS)
CC_FLAGS := -c -fPIC
AR_FLAGS := rcs
STATIC_LIB := lib$(MY_LIB).a
CC := g++
EXEC := test

all: lib main

main:
    $(CC) $(LD_FLAGS) -o $(EXEC)

lib: $(OBJ_FILES)
    ar $(AR_FLAGS) $(STATIC_LIB) $^

obj/SourceFileOpenCVNeeded.o: src/SourceFileOpenCVNeeded.cpp 
    $(CC) $(OCV_PATH) $(CC_FLAGS) $(OCV_LIBS) -c -o $@ $<

obj/OCVAlsoHere.o: src/OCVAlsoHere.cpp 
    $(CC) $(OCV_PATH) $(CC_FLAGS) $(OCV_LIBS) -c -o $@ $<

obj/TheSameForMe.o: src/TheSameForMe.cpp 
    $(CC) $(OCV_PATH) $(CC_FLAGS) $(OCV_LIBS) -c -o $@ $<

obj/%.o: src/%.cpp
    $(CC) $(CC_FLAGS) -c -o $@ $<

clean:
    rm -f $(OBJ_FILES) $(STATIC_LIB) $(EXEC)

PS: any other improvements that can be added to my Makefile?

  • 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-13T20:47:12+00:00Added an answer on June 13, 2026 at 8:47 pm

    You might be able to use target-specific variables, e.g. (not tested)

    EXTRA_FLAGS=
    obj/TheSameForMe.o: EXTRA_FLAGS="$(OCV_PATH) $(OCV_LIBS)"
    
    obj/%.o: src/%.cpp
      $(CC) $(CC_FLAGS) $(EXTRA_FLAGS) -c -o $@ $<
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many source/text file, say file.cpp or file.txt . Now, I want to
I have a problem with some source code regarding OpenSSL and Cpp. For some
I have some source code to get the file name of an url for
I have some source codes to display all file names in a UITablView I
I have to build some C source files using Visual Studio (currently using VS2008)
Some open source libraries have tendency to re implement basic structures like string, list,
I have a c++ project with various extensions for the source files (.cpp, .c,
I need to build some cross platform cpp files in my android mk file.
In my source tree, I have some files with duplicate names. Not really a
I have a project with some folders which happen to contain source files with

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.