Your First Program in C: This tutorial will help you create your first program in the C programming language. Here is what you will need: Windows 7 Operating System (or other) In this tutorial, you will be walked through creating your program using a Windows 7 Operati. Feb 01, 2019 How to Install Dev-C and the GLUT/freeGLUT Libraries for Compiling OpenGL Programs. This tutorial explains how to compile, link, build and run.
GCC (GNU Compiler Collection)A Brief History and Introduction to GCC
The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like operating system as free software, to promote freedom and cooperation among computer users and programmers.
GCC, formerly for 'GNU C Compiler', has grown over times to support many languages such as C (
gcc ), C++ (g++ ), Objective-C, Objective-C++, Java (gcj ), Fortran (gfortran ), Ada (gnat ), Go (gccgo ), OpenMP, Cilk Plus, and OpenAcc. It is now referred to as 'GNU Compiler Collection'. 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:
GCC is portable and run in many operating platforms. GCC (and GNU Toolchain) is currently available on all Unixes. They are also ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is also a cross-compiler, for producing executables on different platform.
GCC Versions
The various GCC versions are:
C++ Standard Support
There are various C++ standards:
The default mode is C++98 for GCC versions prior to 6.1, and C++14 for GCC 6.1 and above. You can use command-line flag
-std to explicitly specify the C++ standard. For example,
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 Terminal, and enter '
gcc --version '. If gcc is not installed, the system will prompt you to install gcc .
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'.
Various GCCs under Cygwin
There are many GCCs under Cygain/MinGW. To differentiate these variations, you need to understand the followings:
MinGW-W64 Target 32/64-bit 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' by selecting these packages (under 'devel' category):
Notes:
Run the executables and check the versions:
Other GCCs in Cygwin
Other GCC packages in Cygwin are:
Post InstallationVersions
You could display the version of GCC via
--version option:
More details can be obtained via
-v option, for example,
HelpHow To Link A File To A Dev-c Project Manager
You can get the help manual via the
--help option. For example,
Man Pages
You can read the GCC manual pages (or man pages) via the
man utility:
Reading man pages under CMD or Bash shell can be difficult. You could generate a text file via:
The
col utility is needed to strip the backspace. (For Cygwin, it is available in 'Utils', 'util-linux' package.)
Alternatively, you could look for an online man pages, e.g., http://linux.die.net/man/1/gcc.
The GCC man pages are kept under '
usr/share/man/man1 '.
Getting Started
The GNU C and C++ compiler are called
gcc and g++ , respectively.
Compile/Link a Simple C Program - hello.c
Below is the Hello-world C program
hello.c :
To compile the
hello.c :
The default output executable is called '
a.exe ' (Windows) or 'a.out ' (Unixes and Mac OS X).
To run the program:
Notes for Unixes and Bash Shell:
To specify the output filename, use
-o option:
NOTE for Unixes:
Compile/Link a Simple C++ Program - hello.cpp
You need to use g++ to compile C++ program, as follows. We use the
-o option to specify the output file name.
More GCC Compiler Options
A few commonly-used GCC compiler options are:
Compile and Link Separately
The above command compile the source file into object file and
link with other object files and system libraries into executable in one step. You may separate compile and link in two steps as follows:
The options are:
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:
R/liluzivert: Lil Uzi Vert. Posted by 6 minutes ago. Lil nuddy verse ruins moneyspread. Too much auto tune and lyrics too generic. Save hide report. Log in or sign up to leave a comment log in sign up. This post has contest mode enabled. Comments are in random ordering and vote scores are hidden. Mar 13, 2020 “No Auto” is a collaboration between rappers Lil Uzi Vert and Lil Durk. The song–which samples Lil Wayne’s “BM J.R.”–was first teased by Lil Durk with a snippet posted to Twitter on. Dec 14, 2017 50+ videos Play all Mix - Lil Uzi Vert - Without Auto-Tune YouTube; Drake. Kodak Black, 21 Savage, Lil Uzi Vert, Lil Yachty & Denzel Curry's 2016 XXL Freshmen Cypher - Duration: 4:13. Lil uzi too much auto tune online. Dec 08, 2016 Auto Tune Vocal Effects (Lil Yachty, Travis Scott, Lil Uzi Vert) If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. Mar 03, 2018 WHEN RAPPERS AUTO-TUNE STOPS WORKING. (Lil Uzi Vert, Quavo, Travis Scott, Lil Yachty & MORE!) - Duration: 10:22. King Trending Recommended for you.
However, we usually 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.
How To Link A File To A Dev-c Project DownloadCompile into a Shared Library
To compile and link C/C++ program into a shared library (
'.dll' in Windows, '.so' in Unixes), use -shared option. Read 'Java Native Interface' for example.
GCC Compilation Process
GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a '
gcc -o hello.exe hello.c ' is carried out as follows:
Verbose Mode (-v)
You can see the detailed compilation process by enabling
-v (verbose) option. For example,
Defining Macro (-D)
You can use the
-Dname option to define a macro, or -Dname=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, .so)Static Library vs. Shared Library
A library is a collection 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.
Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is available.
You can list the contents of a library via '
nm filename '.
How To Link A File To A Dev-c Project PdfSearching for Header Files and Libraries (-I, -L and -l)
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 will not find the headers/libraries unless you set the appropriate options, which is not obvious for first-time user.
For each of the headers used in your source (via
#include directives), the compiler searches the so-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 (e.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 'L' followed by the directory path) (or environment variable LIBRARY_PATH ). In addition, you also have to specify the library name. In Unixes, the library libxxx.a is specified via -lxxx option (lowercase letter 'l' , 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
Try list the default include-paths in your system used by the 'GNU C Preprocessor' via '
cpp -v ':
Try running the compilation in verbose mode (
-v ) to study the library-paths (-L ) and libraries (-l ) used in your system:
Eclipse CDT: In Eclipse CDT, you can set the include paths, library paths and libraries by right-click on the project ⇒ Properties ⇒ C/C++ General ⇒ Paths and Symbols ⇒ Under tabs 'Includes', 'Library Paths' and 'Libraries'. How to remove hyperlinks in dev-c++. The settings are applicable to the selected project only. GCC Environment Variables
GCC uses the following environment variables:
Utilities for Examining the Compiled Files
For all the GNU utilities, you can use '
command --help ' to list the help menu; or 'man command ' to display the man pages.
'file' Utility - Determine File Type
The utility '
file ' can be used to display the type of object files and executable files. For example,
'nm' Utility - List Symbol Table of Object Files
The utility '
nm ' lists symbol table of object files. For example,
'nm' is commonly-used to check if a particular function is defined 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 be resolved by the linker.
'ldd' Utility - List Dynamic-Link Libraries
The utility '
ldd ' examines an executable and displays a list of the shared libraries that it needs. For example,
GNU Make
The '
make ' utility automates the mundane aspects of building executable from source code. 'make ' uses a so-called makefile , which contains rules on how to build the executables.
You can issue '
make --help ' to list the command-line options; or 'man make ' to display the man pages.
First Makefile By Example
Let's begin with a simple example to build the Hello-world program (
hello.c ) into executable (hello.exe ) via make utility.
Create the following file named 'makefile' (without any file extension), which contains rules to build the executable, and save in the same directory as the source file. Use 'tab' to indent the command (NOT spaces).
Run the '
make ' utility as follows:
Running
make without argument starts the target 'all ' in the makefile . A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requisites and a command, as follows:
The target and pre-requisites are separated by a colon (
: ). The command must be preceded by 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 above example, the rule '
all ' has a pre-requisite 'hello.exe '. make cannot find the file 'hello.exe ', so it looks for a rule to create it. The rule 'hello.exe ' has a pre-requisite 'hello.o '. Again, it does not exist, so make looks for a rule to create it. The rule 'hello.o ' has a pre-requisite 'hello.c '. make checks that 'hello.c ' exists and it is newer than the target (which does not exist). It runs the command 'gcc -c hello.c '. The rule 'hello.exe ' then run its command 'gcc -o hello.exe hello.o '. Finally, the rule 'all ' does nothing.
More importantly, if the pre-requisite is not newer than than target, the command will not be run. In other words, the command will be run only if the target is out-dated compared with its pre-requisite. For example, if we re-run the make command:
You can also specify the target to be made in the
make command. For example, the target 'clean ' removes the 'hello.o ' and 'hello.exe '. You can then run the make without target, which is the same as 'make all '.
How To Link A File To A Dev-c Project For Beginners
Try modifying the '
hello.c ' and run make .
NOTES:
More on MakefileComment & Continuation
A comment begins with a
# and lasts till the end of the line. Long line can be broken and continued in several lines via a back-slash ( ).
Syntax of Rules
A general syntax for the rules is:
The rules are usually organized in such as way the more general rules come first. The overall rule is often name '
all ', which is the default target for make .
Phony Targets (or Artificial Targets)
A target that does not represent a file is called 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 against its pre-requisite for out-of-date-ness. Phony target is always out-of-date and its command will be run. The standard phony targets are: all , clean , install .
Variables
A variable begins with a
$ and is enclosed within parentheses (..) or braces {..} . Single character variables do not need the parentheses. For example, $(CC) , $(CC_FLAGS) , $@ , $^ .
Automatic Variables
Automatic variables are set by make after a rule is matched. There include:
For example, we can rewrite the earlier makefile as:
Virtual Path - VPATH & vpath
You can use
VPATH (uppercase) to specify the directory to search for dependencies and target files. For example,
You can also use
vpath (lowercase) to be more precise about the file type and its search directory. For example,
Pattern Rules
A pattern rule, which uses pattern matching character
'%' as the filename, can be applied to create a target, if there is no explicit rule. For example,
Implicit Pattern Rules
Make comes with a huge set of implicit pattern rules. You can list all the rule via
--print-data-base option.
A Sample Makefile
This sample makefile is extracted from Eclipse's 'C/C++ Development Guide -Makefile'.
Brief Summary
I have presented the basic make features here so that you can read and understand simple makefiles for building C/C++ applications. Make is actually quite complex, and can be considered as a programming language by itself!!
REFERENCES & RESOURCES
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |