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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:19:17+00:00 2026-05-23T17:19:17+00:00

I’ve been using c++ casually for a couple of months with a makefile I

  • 0

I’ve been using c++ casually for a couple of months with a makefile I don’t even remember where I got or if I did it myself (the actual structure it has. I know I’ve added some libraries and flags though):

SRC = filename.cpp
TARG = filename

CC = g++

CPPFLAGS = -g -Wall -Werror -Wextra -pipe -pedantic -Weffc++ -std=c++0x -O2 -Wno-unused-function `pkg-config --cflags opencv`

LDFLAGS = `pkg-config --libs opencv` -lboost_regex -lboost_filesystem

OBJ = $(SRC:.cpp=.o)

all: $(TARG)

clean:
    rm -f *~ *.o $(TARG)

I wanted to use it to compile a class, but first I have to understand what is going on since I have to modify it a bit. Also, are there any bad practices in it?

  • 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-23T17:19:18+00:00Added an answer on May 23, 2026 at 5:19 pm

    If you want to compile a class instead of a program, you need to do a little surgery on the file. I’m assuming that you want an object file, not a library; the rules for libraries are more complex. But this should do the trick.

    SRC = class.cpp
    OBJ = $(SRC:.cpp=.o)   
    CC  = g++
    CPPFLAGS = -g -Wall -Werror -Wextra -pipe -pedantic -Weffc++ -std=c++0x -O2 \
               -Wno-unused-function `pkg-config --cflags opencv`
    LDFLAGS = `pkg-config --libs opencv` -lboost_regex -lboost_filesystem
    DEBRIS  = core a.out *~
    
    all: $(OBJ)
    
    class.o: class.h
    
    clean:
        rm -f $(DEBRIS) $(OBJ)
    

    It is not immediately clear whether $(CPPFLAGS) will automatically appear in the compilation or linking commands.

    If you end up wanting to build the program from two files, then you use a hybrid:

    SRC      = filename.cpp class.cpp
    OBJ      = $(SRC:.cpp=.o)
    PROGRAM  = program
    CXX      = g++
    CXXFLAGS = -g -Wall -Werror -Wextra -pipe -pedantic -Weffc++ -std=c++0x -O2 \
               -Wno-unused-function `pkg-config --cflags opencv`
    LDFLAGS  = `pkg-config --libs opencv` -lboost_regex -lboost_filesystem
    DEBRIS   = core a.out *~
    
    all: $(PROGRAM)
    
    $(PROGRAM): $(OBJ)
        $(CXX) $(CXXFLAGS) -o $@ $(OBJ) $(LDFLAGS)
    
    class.o: class.h
    filename.cpp: class.h
    
    clean:
        rm -f $(DEBRIS) $(OBJ) $(PROGRAM)
    

    Note that I’ve changed the macro for the C++ compiler from CC to CXX. The standards are not clear on the name for the C++ compiler. POSIX doesn’t mention C++ in its description of make. However, CC is clearly intended for compiling C rather than C++. That needn’t stop your version of make from using CC for C++ compilation, but it would be a little unusual. (GNU Make 3.81 on MacOS X 10.6.8 uses CXX for the C++ compilation.) The link line now uses $(CXXFLAGS) (thanks to eriktous); it is still not clear whether the C++ source to object file compilation would do so. Ultimately, that’s why you end up seeing makefiles with rules such as:

    class.o: class.h
        $(CXX) -c $(CXXFLAGS) $*.cpp
    

    This guarantees that the compilation rule is what you see for this object file. You might write that as an old-fashioned but portable suffix rule (.cpp.o:) instead; the POSIX specification for make supports these. Or you might use the more modern but not necessarily quite as portable %.o : %.o notation instead (not required by POSIX). Either of these replaces any previous (built-in) definition of how to compile an object file from C++ source.

    .cpp.o:
        $(CXX) -c $(CXXFLAGS) $*.cpp
    
    %.o : %.cpp
        $(CXX) -c $(CXXFLAGS) $*.cpp
    

    I assume you are using opencv (and some of Boost); if not, your compilation and linking flags include irrelevant options. The dependencies are guessed; make will infer the dependency of the object file on the C++ source code, so I only listed header dependencies. But you may have many more if you’re using opencv and Boost.

    (Makefiles not formally tested.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.