Openmp Makefile Dont Compile Again and Again

GCC and Make

Compiling, Linking and Building
C/C++ Applications

GCC (GNU Compiler Collection)

A Brief History and Introduction to GCC

The original GNU C Compiler (GCC) is adult by Richard Stallman, the founder of the GNU Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like operating organization every bit free software, to promote liberty and cooperation amid estimator users and programmers.

GCC, formerly for "GNU C Compiler", has grown over times to support many languages such as C (gcc), C++ (chiliad++), Objective-C, Objective-C++, Coffee (gcj), Fortran (gfortran), Ada (gnat), Go (gccgo), OpenMP, Cilk Plus, and OpenAcc. It is at present referred to as "GNU Compiler Drove". The mother site for GCC is http://gcc.gnu.org/. The current version is GCC 7.3, released on 2018-01-25.

GCC is a key component of so-called "GNU Toolchain", for developing applications and writing operating systems. The GNU Toolchain includes:

  1. GNU Compiler Collection (GCC): a compiler suite that supports many languages, such as C/C++ and Objective-C/C++.
  2. GNU Make: an automation tool for compiling and building applications.
  3. GNU Binutils: a suite of binary utility tools, including linker and assembler.
  4. GNU Debugger (GDB).
  5. GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.
  6. GNU Bison: a parser generator (similar to lex and yacc).

GCC is portable and run in many operating platforms. GCC (and GNU Toolchain) is currently bachelor on all Unixes. They are besides ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is as well a cross-compiler, for producing executables on different platform.

GCC Versions

The various GCC versions are:

  • GCC version 1 (1987): Initial version that support C.
  • GCC version ii (1992): supports C++.
  • GCC version 3 (2001): incorporating ECGS (Experimental GNU Compiler System), with improve optimization.
  • GCC version four (2005):
  • GCC version 5 (2015):
  • GCC Version 6 (2016):
  • GCC Version 7 (2017):
C++ Standard Support

There are various C++ standards:

  • C++98
  • C++11 (aka C++0x)
  • C++14 (aka C++1y)
  • C++17 (aka C++1z)
  • C++2a (next planned standard in 2020)

The default style is C++98 for GCC versions prior to six.i, and C++14 for GCC half dozen.1 and in a higher place. You lot can utilise command-line flag -std to explicitly specify the C++ standard. For instance,

  • -std=c++98, or -std=gnu++98 (C++98 with GNU extensions)
  • -std=c++xi, or -std=gnu++eleven (C++11 with GNU extensions)
  • -std=c++14, or -std=gnu++14 (C++14 with GNU extensions), default mode for GCC 6.i and to a higher place.
  • -std=c++17, or -std=gnu++17 (C++17 with GNU extensions), experimental.
  • -std=c++2a, or -std=gnu++2a (C++2a with GNU extensions), experimental.

Installing GCC on Unixes

GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems.

Installing GCC on Mac OS X

Open a Final, and enter "gcc --version". If gcc is not installed, the system will prompt you to install gcc.

$          gcc --version          ...... Target: x86_64-apple-darwin14.five.0    Thread model: posix

Installing GCC on Windows

For Windows, you could either install Cygwin GCC, MinGW GCC or MinGW-W64 GCC. Read "How to install Cygwin and MinGW".

  • Cygwin GCC: Cygwin is a Unix-like environs and control-line interface for Microsoft Windows. Cygwin is huge and includes most of the Unix tools and utilities. Information technology also included the unremarkably-used Bash beat out.
  • MinGW: MinGW (Minimalist GNU for Windows) is a port of the GNU Compiler Collection (GCC) and GNU Binutils for use in Windows. It besides included MSYS (Minimal System), which is basically a Bourne shell (bash ).
  • MinGW-W64: a fork of MinGW that supports both 32-flake and 64-fleck windows.
Various GCCs nether Cygwin

At that place are many GCCs under Cygain/MinGW. To differentiate these variations, you lot need to sympathize the followings:

  • Windows/Intel uses these instruction sets: x86 is a 32-flake instruction fix; i868 is a 32-bit enhanced version of x86; x86_64 (or amd64) is a 64-bit instruction set.
  • 32-chip compilers/programs can run on 32-flake or 64-bit (astern compatible) Windows, but 64-flake compiler can only run on 64-flake Windows.
  • 64-bit compilers may produce target of 32-bit or 64-flake.
  • If you use Cygwin's GCC, the target could be native Windows or Cygwin. If the target is native Windows, the code can be distributed and run under Windows. Yet, if the target is Cygwin, to distribute, you demand to distribute Cygwin runtime environment (cygwin1.dll). This is because Cygwin is a Unix emulator under Windows.
MinGW-W64 Target 32/64-flake Native Windows

The MinGW-W64 (a fork of MinGW, available at http://mingw-w64.org/doku.php) supports target of both 32-bit and 64-bit native Windows. You can install "MinGW-W64" under "Cygwin" past selecting these packages (under "devel" category):

  • mingw64-x86_64-gcc-cadre: 64-bit C compiler for target of native 64-bit Windows. The executable is "x86_64-w64-mingw32-gcc".
  • mingw64-x86_64-gcc-g++: 64-chip C++ compiler for target of native 64-flake Windows. The executable is "x86_64-w64-mingw32-g++".
  • mingw64-i686-gcc-core: 64-scrap C compiler for target of native 32-bit Windows. The executable is "i686-w64-mingw32-gcc".
  • mingw64-i686-gcc-g++: 64-bit C++ compiler for target of native 32-chip Windows. The executable is "i686-w64-mingw32-g++".

Notes:

  • I suggest y'all install "mingw64-x86_64-gcc-core" and "mingw64-x86_64-gcc-g++" to provide native 64-fleck Windows codes, but skip "mingw64-i686-gcc-core" and "mingw64-i686-gcc-thousand++", unless you need to produce 32-bit Windows applications.
  • For JNI (Coffee Native Interface) in 64-flake Java, you need to use "x86_64-w64-mingw32-gcc" or "x86_64-w64-mingw32-m++" to produce 64-bit native Windows code.

Run the executables and check the versions:

            $          x86_64-w64-mingw32-gcc --version          
x86_64-w64-mingw32-gcc (GCC) 6.four.0 $ x86_64-w64-mingw32-gcc -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/6.four.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: .....
Thread model: posix
gcc version 6.iv.0 (GCC) $ x86_64-w64-mingw32-g++ --version
x86_64-w64-mingw32-k++ (GCC) half dozen.4.0 $ i686-w64-mingw32-gcc --version
i686-w64-mingw32-gcc (GCC) vi.4.0
$ i686-w64-mingw32-g++ --version
i686-w64-mingw32-g++ (GCC) six.4.0
Other GCCs in Cygwin

Other GCC packages in Cygwin are:

  • gcc-core, gcc-chiliad++: Basic 64-scrap C/C++ compiler target 64-bit Cygwin. You probably should install these ii packages likewise. Even so, to distribute the code produced, y'all need to distribute Cygwin Runtime Environment (cygwin1.dll). This is considering Cygwin is a Unix emulator under Windows.
  • cygwin32-gcc-core, cygwin32-gcc-g++: Older 32-scrap C/C++ compiler for target 32-chip Cygwin (Obsoleted by gcc-code and gcc-g++?).
  • mingw-gcc-core, mingw-gcc-thousand++: Older MinGW 32-bit C/C++ compiler for 32-flake Windows (Obsoleted by MinGW-W64 packages?).

Post Installation

Versions

Yous could display the version of GCC via --version option:

$          gcc --version          gcc (GCC) six.iv.0  $          k++ --version          m++ (GCC) 6.4.0

More details can be obtained via -five option, for instance,

$          gcc -v          Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: ......
Thread model: posix
gcc version 6.four.0 (GCC) $ chiliad++ -5
Using built-in specs.
COLLECT_GCC=m++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: ......
Thread model: posix
gcc version 6.four.0 (GCC)
Help

You tin go the assist manual via the --help selection. For example,

$          gcc --aid        
Man Pages

Y'all can read the GCC manual pages (or man pages) via the homo utility:

$          human gcc           $          man m++        

Reading homo pages under CMD or Bash beat out can be hard. You lot could generate a text file via:

$          man gcc | col -b > gcc.txt        

The col utility is needed to strip the backspace. (For Cygwin, information technology is bachelor in "Utils", "util-linux" package.)

Alternatively, you could look for an online man pages, east.g., http://linux.dice.cyberspace/human/one/gcc.

The GCC human pages are kept under "usr/share/man/man1".

$          whereis gcc          gcc: /usr/bin/gcc.exe /usr/lib/gcc /usr/share/human being/man1/gcc.i.gz

Getting Started

The GNU C and C++ compiler are called gcc and thousand++, respectively.

Compile/Link a Simple C Programme - hello.c

Below is the How-do-you-do-world C plan hello.c:

ane ii three 4 5 6 7
                    #include <stdio.h>   int main() {     printf("Hello, world!\n");     return 0; }

To compile the hello.c:

>          gcc how-do-you-do.c        

The default output executable is chosen "a.exe" (Windows) or "a.out" (Unixes and Mac Os X).

To run the program:

            >          a           $          chmod a+x a.out          $          ./a.out        

Notes for Unixes and Bash Shell:

  • In Bash shell, the default PATH does not include the electric current working directory. Hence, you need to include the current path (./) in the command. (Windows include the current directory in the PATH automatically; whereas Unixes do non - yous need to include the current directory explicitly in the PATH.)
  • Yous also need to include the file extension, if any, i.east., "./a.out".
  • In Unixes, the output file could be "a.out" or simply "a". Furthermore, y'all need to assign executable file-way (x) to the executable file "a.out", via control "chmod a+x filename " (add executable file-mode "+10" to all users "a+ten").

To specify the output filename, apply -o choice:

            >          gcc -o hello.exe hello.c           >          hello             $          gcc -o hi how-do-you-do.c          $          chmod a+x hello          $          ./hello        

Notation for Unixes:

  • In Unixes, we typically omit the .exe file extension (meant for Windows only), and merely name the output executable as hello (via command "gcc -o hello hullo.c".
  • You need to assign executable file style via command "chmod a+ten hi".
Compile/Link a Simple C++ Programme - hello.cpp
one 2 three four v half dozen 7 8
#include <iostream> using namespace std;   int master() {    cout << "Hello, world!" << endl;    return 0; }

Yous need to use g++ to compile C++ programme, equally follows. We use the -o option to specify the output file proper noun.

            >          g++ -o hello.exe hello.cpp           >          hello             $          one thousand++ -o hello hi.cpp          $          chmod a+x how-do-you-do          $          ./hullo        
More GCC Compiler Options

A few ordinarily-used GCC compiler options are:

$ thousand++          -Wall -g -o Hello.exe          Hello.cpp
  • -o: specifies the output executable filename.
  • -Wall: prints "all" Westwardarning letters.
  • -g: generates additional symbolic debugthousandging information for use with gdb debugger.
Compile and Link Separately

The above control compile the source file into object file and link with other object files and system libraries into executable in 1 step. You lot may separate compile and link in two steps as follows:

            >          yard++ -c -Wall -thousand Hello.cpp           >          g++ -1000 -o Hello.exe Hello.o        

The options are:

  • -c: Compile into object file "Hullo.o". By default, the object file has the aforementioned proper noun every bit the source file with extension of ".o" (in that location is no demand to specify -o selection). No linking with other object files or libraries.
  • Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld.exe) to perform the linking.
Compile and Link Multiple Source Files

Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:

> chiliad++ -o myprog.exe file1.cpp file2.cpp        

However, we unremarkably compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.

>          grand++ -c file1.cpp          >          g++ -c file2.cpp          >          thou++ -o myprog.exe file1.o file2.o        
Compile into a Shared Library

To compile and link C/C++ program into a shared library (".dll" in Windows, ".so" in Unixes), use -shared choice. Read "Java Native Interface" for case.

GCC Compilation Process

GCC compiles a C/C++ program into executable in iv steps equally shown in the above diagram. For example, a "gcc -o hello.exe hullo.c" is carried out as follows:

  1. Pre-processing: via the GNU C Preprocessor (cpp.exe), which includes the headers (#include) and expands the macros (#define).
    >              cpp hello.c > hello.i            
    The resultant intermediate file "hello.i" contains the expanded source code.
  2. Compilation: The compiler compiles the pre-processed source code into assembly code for a specific processor.
    >              gcc -S hello.i            
    The -S choice specifies to produce assembly lawmaking, instead of object code. The resultant assembly file is "hello.south".
  3. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hi.o".
    >              as -o hello.o hello.s            
  4. Linker: Finally, the linker (ld.exe) links the object code with the library lawmaking to produce an executable file "hello.exe".
    >              ld -o hello.exe hello.o ...libraries...            
Verbose Mode (-v)

You tin can see the detailed compilation procedure by enabling -v (verbose) option. For example,

>          gcc -v -o hello.exe hello.c        
Defining Macro (-D)

You can utilize the -Dproper name option to define a macro, or -Dproper noun=value to define a macro with a value. The value should be enclosed in double quotes if it contains spaces.

Headers (.h), Static Libraries (.lib, .a) and Shared Library (.dll, .then)

Static Library vs. Shared Library

A library is a drove of pre-compiled object files that can be linked into your programs via the linker. Examples are the system functions such as printf() and sqrt().

There are two types of external libraries: static library and shared library.

  1. A static library has file extension of ".a" (archive file) in Unixes or ".lib" (library) in Windows. When your plan is linked against a static library, the automobile code of external functions used in your program is copied into the executable. A static library can be created via the annal program "ar.exe".
  2. A shared library has file extension of ".so" (shared objects) in Unixes or ".dll" (dynamic link library) in Windows. When your program is linked against a shared library, just a small table is created in the executable. Before the executable starts running, the operating organization loads the machine code needed for the external functions - a process known as dynamic linking. Dynamic linking makes executable files smaller and saves disk space, because one re-create of a library can be shared betwixt multiple programs. Furthermore, most operating systems allows one copy of a shared library in memory to be used past all running programs, thus, saving retentiveness. The shared library codes tin exist upgraded without the demand to recompile your program.

Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is bachelor.

You lot can list the contents of a library via "nm filename ".

Searching for Header Files and Libraries (-I, -50 and -50)

When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker volition not detect the headers/libraries unless you set the advisable options, which is not obvious for offset-time user.

For each of the headers used in your source (via #include directives), the compiler searches the and then-called include-paths for these headers. The include-paths are specified via -Idir option (or environment variable CPATH). Since the header's filename is known (east.g., iostream.h, stdio.h), the compiler only needs the directories.

The linker searches the so-called library-paths for libraries needed to link the program into an executable. The library-path is specified via -Ldir option (uppercase '50' followed by the directory path) (or environment variable LIBRARY_PATH). In addition, y'all likewise have to specify the library name. In Unixes, the library libxxx.a is specified via -lthirty pick (lowercase letter of the alphabet 'fifty', without the prefix "lib" and ".a" extension). In Windows, provide the full name such as -lxxx.lib. The linker needs to know both the directories as well as the library names. Hence, two options need to be specified.

Default Include-paths, Library-paths and Libraries

Endeavour list the default include-paths in your system used by the "GNU C Preprocessor" via "cpp -5":

>          cpp -5          ...... #include "..." search starts here: #include <...> search starts here:  /usr/lib/gcc/x86_64-pc-cygwin/vi.4.0/include
/usr/include
/usr/lib/gcc/x86_64-pc-cygwin/half dozen.4.0/../../../../lib/../include/w32api

Try running the compilation in verbose mode (-v) to study the library-paths (-Fifty) and libraries (-l) used in your arrangement:

>          gcc -v -o hello.exe how-do-you-do.c          ...... -L/usr/lib/gcc/x86_64-pc-cygwin/vi.iv.0 -L/usr/x86_64-pc-cygwin/lib -L/usr/lib -L/lib -lgcc_s      -lgcc        -lcygwin     -ladvapi32   -lshell32    -luser32     -lkernel32        


Eclipse CDT: In Eclipse CDT, you tin set the include paths, library paths and libraries by right-click on the projection ⇒ Properties ⇒ C/C++ Full general ⇒ Paths and Symbols ⇒ Under tabs "Includes", "Library Paths" and "Libraries". The settings are applicable to the selected project only.

GCC Environment Variables

GCC uses the following environment variables:

  • PATH: For searching the executables and run-fourth dimension shared libraries (.dll, .and so).
  • CPATH: For searching the include-paths for headers. Information technology is searched afterward paths specified in -I<dir> options. C_INCLUDE_PATH and CPLUS_INCLUDE_PATH tin can be used to specify C and C++ headers if the item language was indicated in pre-processing.
  • LIBRARY_PATH: For searching library-paths for link libraries. It is searched after paths specified in -50<dir> options.

Utilities for Examining the Compiled Files

For all the GNU utilities, you tin use " command --help" to list the help carte; or "human being command " to display the man pages.

"file" Utility - Make up one's mind File Type

The utility "file" tin can be used to display the type of object files and executable files. For example,

$          gcc -c hello.c          $          gcc -o hello.exe hello.o          $          file howdy.c          
hi.c: C source, ASCII text, with CRLF line terminators $ file hello.o hi.o: information > file hello.exe hullo.exe: PE32 executable (console) x86-64, for MS Windows
"nm" Utility - Listing Symbol Tabular array of Object Files

The utility "nm" lists symbol tabular array of object files. For instance,

$          nm hello.o          0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
U __main
0000000000000000 T master
U puts $ nm hello.exe | grep main
00000001004080cc I __imp___main
0000000100401120 T __main
00000001004010e0 T primary
......

"nm" is usually-used to check if a detail function is divers in an object file. A 'T' in the second column indicates a function that is defined, while a 'U' indicates a function which is undefined and should exist resolved past the linker.

"ldd" Utility - List Dynamic-Link Libraries

The utility "ldd" examines an executable and displays a list of the shared libraries that information technology needs. For case,

>          ldd how-do-you-do.exe          ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ff9ba3c0000)
KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ff9b9880000)
KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ff9b6a60000)
SYSFER.DLL => /cygdrive/c/WINDOWS/System32/SYSFER.DLL (0x6ec90000)
ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ff9b79a0000)
msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ff9b9100000)
sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ff9b9000000)
RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ff9b9700000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)

GNU Brand

The "brand" utility automates the mundane aspects of building executable from source lawmaking. "make" uses a then-called makefile, which contains rules on how to build the executables.

You can issue "brand --aid" to list the command-line options; or "man make" to display the human being pages.

First Makefile By Example

Let'southward brainstorm with a simple case to build the Howdy-world program (hullo.c) into executable (hello.exe) via brand utility.

1 two 3 four five six 7
                    #include <stdio.h>   int main() {     printf("Hello, world!\due north");     return 0; }

Create the following file named "makefile" (without any file extension), which contains rules to build the executable, and relieve in the aforementioned directory every bit the source file. Use "tab" to indent the command (NOT spaces).

all: hello.exe  hi.exe: hi.o 	 gcc -o how-do-you-do.exe hello.o  hello.o: howdy.c 	 gcc -c hello.c       clean: 	 rm hello.o hello.exe

Run the "make" utility as follows:

>          make          gcc -c hello.c gcc -o hello.exe hello.o

Running brand without argument starts the target "all" in the makefile. A makefile consists of a set of rules. A dominion consists of iii parts: a target, a list of pre-requisites and a command, as follows:

          target:          pre-req-1          pre-req-2          ...          command        

The target and pre-requisites are separated by a colon (:). The command must be preceded past a tab (Not spaces).

When make is asked to evaluate a rule, it begins by finding the files in the prerequisites. If any of the prerequisites has an associated rule, make attempts to update those first.

In the to a higher place example, the rule "all" has a pre-requisite "how-do-you-do.exe". make cannot observe the file "hello.exe", and then it looks for a dominion to create it. The dominion "hello.exe" has a pre-requisite "hello.o". Again, it does not exist, then brand looks for a rule to create it. The rule "hi.o" has a pre-requisite "howdy.c". make checks that "hi.c" exists and it is newer than the target (which does not exist). Information technology runs the command "gcc -c hullo.c". The rule "how-do-you-do.exe" and so run its control "gcc -o how-do-you-do.exe hello.o". Finally, the rule "all" does nil.

More chiefly, if the pre-requisite is non newer than than target, the command volition not be run. In other words, the command volition be run but if the target is out-dated compared with its pre-requisite. For instance, if we re-run the make command:

>          make          make: Nothing to exist done for `all'.

You can also specify the target to be made in the make command. For example, the target "clean" removes the "hullo.o" and "howdy.exe". Yous can then run the make without target, which is the same equally "make all".

>          brand make clean          rm how-do-you-do.o how-do-you-do.exe   >          make          gcc -c how-do-you-do.c gcc -o hello.exe howdy.o

Try modifying the "hello.c" and run make.

NOTES:

  • If the command is not preceded by a tab, you become an error message "makefile:4: *** missing separator. Finish."
  • If there is no makefile in the electric current directory, you get an fault message "make: *** No targets specified and no makefile found. Stop."
  • The makefile can be named "makefile", "Makefile" or "GNUMakefile", without file extension.

More on Makefile

Comment & Continuation

A comment begins with a # and lasts till the end of the line. Long line can exist broken and connected in several lines via a back-slash (\).

Syntax of Rules

A general syntax for the rules is:

          target1          [target2          ...]: [pre-req-1          pre-req-two          ...] 	[command1          command2          ......]

The rules are usually organized in such as way the more general rules come first. The overall rule is ofttimes proper name "all", which is the default target for make.

Phony Targets (or Artificial Targets)

A target that does non represent a file is chosen a phony target. For example, the "clean" in the above example, which is just a label for a command. If the target is a file, it will be checked confronting its pre-requisite for out-of-engagement-ness. Phony target is always out-of-engagement and its command will exist run. The standard phony targets are: all, clean, install.

Variables

A variable begins with a $ and is enclosed inside parentheses (...) or braces {...}. Unmarried graphic symbol variables do not demand the parentheses. For instance, $(CC), $(CC_FLAGS), $@, $^.

Automated Variables

Automatic variables are set by brand after a rule is matched. There include:

  • $@: the target filename.
  • $*: the target filename without the file extension.
  • $<: the first prerequisite filename.
  • $^: the filenames of all the prerequisites, separated by spaces, discard duplicates.
  • $+: similar to $^, but includes duplicates.
  • $?: the names of all prerequisites that are newer than the target, separated past spaces.

For instance, we can rewrite the earlier makefile as:

all: hello.exe   # $@ matches the target; $< matches the first dependent hello.exe: hello.o 	gcc -o $@ $<  hello.o: hello.c 	gcc -c $<       clean: 	rm hello.o hello.exe
Virtual Path - VPATH & vpath

You can use VPATH (uppercase) to specify the directory to search for dependencies and target files. For case,

            VPATH = src include

You can also utilize vpath (lowercase) to exist more precise about the file type and its search directory. For instance,

            vpath %.c src vpath %.h include
Pattern Rules

A pattern rule, which uses blueprint matching character '%' equally the filename, tin be applied to create a target, if there is no explicit rule. For example,

            %.o: %.c 	$(COMPILE.c) $(OUTPUT_OPTION) $<    %: %.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
Implicit Pattern Rules

Brand comes with a huge set of implicit pattern rules. You tin can list all the dominion via --impress-data-base option.

A Sample Makefile

This sample makefile is extracted from Eclipse'south "C/C++ Development Guide -Makefile".

               LINK_TARGET = test_me.exe  OBJS =  \  Test1.o \  Test2.o \  Main.o   REBUILDABLES = $(OBJS) $(LINK_TARGET)   clean :    rm -f $(REBUILDABLES)   echo Clean washed       all : $(LINK_TARGET)   echo All washed     $(LINK_TARGET) : $(OBJS)   one thousand++ -g -o $@ $^   %.o : %.cpp   g++ -g -o $@ -c $<   Main.o : Chief.h Test1.h Test2.h Test1.o : Test1.h Test2.h Test2.o : Test2.h   # %.dep : %.cpp #   thousand++ -M $(FLAGS) $< > $@ # include $(OBJS:.o=.dep)

Brief Summary

I accept presented the bones brand features here so that you tin read and understand unproblematic makefiles for edifice C/C++ applications. Make is really quite complex, and can be considered as a programming language by itself!!

REFERENCES & RESOURCES

  1. GCC Transmission "Using the GNU Compiler Collection (GCC)" @ http://gcc.gnu.org/onlinedocs.
  2. GNU 'brand' manual @ http://www.gnu.org/software/make/manual/brand.html.
  3. Robert Mecklenburg, "Managing Projects with GNU Make", 3rd Edition, 2004.

Latest version tested: CygWin GCC 6.4.0, MinGW-W64 GCC 6.iv.0
Final modified: March, 2018

housecumeneamord.blogspot.com

Source: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

0 Response to "Openmp Makefile Dont Compile Again and Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel