C++, the “Old Fashioned” way

前端 未结 8 1044
孤街浪徒
孤街浪徒 2021-02-04 14:56

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 under

8条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 15:46

    A simple example is often useful to show the basic procedure, so:

    Sample gcc usage to compile C++ files:

    $ g++ -c file1.cpp                 # compile object files
    [...]
    $ g++ -c file2.cpp
    [...]
    $ g++ -o program file1.o file2.o   # link program
    [...]
    $ ./program                        # run program
    

    To use make to do this build, the following Makefile could be used:

    # main target, with dependencies, followed by build command (indented with )
    program: file1.o file2.o
        g++ -o program file1.o file2.o
    
    # rules for object files, with dependencies and build commands
    file1.o: file1.cpp file1.h
        g++ -c file1.cpp
    
    file2.o: file2.cpp file2.h file1.h
        g++ -c file2.cpp
    

    Sample Makefile usage:

    $ make                              # build it
    [...]
    $ ./program                         # run it
    

    For all the details you can look at the Gnu make manual and GCC's documentation.

提交回复
热议问题