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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:45:57+00:00 2026-05-13T11:45:57+00:00

I have been learning C++ in school to create small command-line programs. However, I

  • 0

I have been learning C++ in school to create small command-line programs.

However, I have only built my projects with IDEs, including VS08 and QtCreator.

I understand the process behind building a project: compile source to object code, then link them into an executable that is platform specific (.exe, .app, etc). I also know most projects also use make to streamline the process of compiling and linking multiple source and header files.

The thing is, although IDEs do all this under the hood, making life very easy, I don’t really know what is really happening, and feel that I need to get accustomed to building projects the “old fashioned way”: from the command line, using the tool chain explicitly.

I know what Makefiles are, but not how to write them.
I know what gcc does, but not how to use it.
I know what the linker does, but not how to use it.

What I am looking for, is either an explanation, or link to a tutorial that explains, the workflow for a C++ project, from first writing the code up to running the produced executable.

I would really like to know the what, how, and why of building C++.

(If it makes any difference, I am running Mac OS X, with gcc 4.0.1 and make 3.81)

Thanks!

  • 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-13T11:45:58+00:00Added an answer on May 13, 2026 at 11:45 am

    Compiling

    Let’s say you want to write a simple ‘hello world’ application. You have 3 files, hello.cpp hello-writer.cpp and hello-writer.h, the contents being

    // hello-writer.h
    void WriteHello(void);
    
    // hello-writer.cpp
    #include "hello-writer.h"
    #include <stdio>
    void WriteHello(void){
        std::cout<<"Hello World"<<std::endl;
    }
    
    // hello.cpp
    #include "hello-writer.h"
    int main(int argc, char ** argv){
        WriteHello();
    }
    

    The *.cpp files are converted to object files by g++, using the commands

    g++ -c hello.cpp -o hello.o
    g++ -c hello-writer.cpp -o hello-writer.o
    

    The -c flag skips the linking for the moment. To link all the modules together requires running

    g++ hello.o hello-writer.o -o hello
    

    creating the program hello. If you need to link in any external libraries you add them to this line, eg -lm for the math library. The actual library files would look something like libm.a or libm.so, you ignore the suffix and the ‘lib’ part of the filename when adding the linker flag.

    Makefile

    To automate the build process you use a makefile, which consists of a series of rules, listing a thing to create and the files needed to create it. For instance, hello.o depends on hello.cpp and hello-writer.h, its rule is

    hello.o:hello.cpp hello-writer.h
         g++ -c hello.cpp -o hello.o # This line must begin with a tab.
    

    If you want to read the make manual, it tells you how to use variables and automatic rules to simplify things. You should be able to just write

    hello.o:hello.cpp hello-writer.h
    

    and the rule will be created automagically. The full makefile for the hello example is

    all:hello
    hello:hello.o hello-writer.o
        g++ hello.o hello-writer.o -o hello
    hello.o:hello.cpp hello-writer.h
        g++ -c hello.cpp -o hello.o
    hello-writer.o:hello-writer.cpp hello-writer.h
        g++ -c hello-writer.cpp -o hello-writer.o
    

    Remember that indented lines must start with tabs. Not that not all rules need an actual file, the all target just says create hello. It is common for this to be the first rule in the makefile, the first being automatically created when you run make.

    With all this set up you should then be able to go to a command line and run

    $ make
    $ ./hello
    Hello World
    

    More advanced Makefile stuff

    There are also some useful variables that you can define in your makefile, which include

    • CXX: c++ compiler
    • CXXFLAGS:
      Additional flags to pass to the
      compiler (E.g include directories
      with -I)
    • LDFLAGS: Additional flags to
      pass to the linker
    • LDLIBS: Libraries
      to link
    • CC: c compiler (also used to
      link)
    • CPPFLAGS: preprocessor flags

    Define variables using =, add to variables using +=.

    The default rule to convert a .cpp file to a .o file is

    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@
    

    where $< is the first dependancy and $@ is the output file. Variables are expanded by enclosing them in $(), this rule will be run with the pattern hello.o:hello.cpp

    Similarly the default linker rule is

    $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
    

    where $^ is all of the prerequisites. This rule will be run with the pattern hello:hello.o hello-writer.o. Note that this uses the c compiler, if you don’t want to override this rule and are using c++ add the library -lstdc++ to LDLIBS with the line

    LDLIBS+=-lstdc++
    

    in the makefile.

    Finally, if you don’t list the dependancies of a .o file make can find them itself, so a minimal makefile might be

    LDFLAGS=-lstdc++
    all:hello
    hello:hello.o hello-writer.o
    

    Note that this ignores the dependancy of the two files on hello-writer.h, so if the header is modified the program won’t be rebuilt. If you’re interested, check the -MD flag in the gcc docs for how you can automatically generate this dependancy.

    Final makefile

    A reasonable final makefile would be

    // Makefile
    CC=gcc
    CXX=g++
    CXXFLAGS+=-Wall -Wextra -Werror
    CXXFLAGS+=-Ipath/to/headers
    LDLIBS+=-lstdc++ # You could instead use CC = $(CXX) for the same effect 
                     # (watch out for c code though!)
    
    all:hello                                   # default target
    hello:hello.o hello-world.o                 # linker
    hello.o:hello.cpp hello-world.h             # compile a module
    hello-world.o:hello-world.cpp hello-world.h # compile another module
        $(CXX) $(CXXFLAGS) -c $< -o $@          # command to run (same as the default rule)
                                                # expands to g++ -Wall ... -c hello-world.cpp -o hello-world.o
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.