o object files created by the compiler: unix> ls -ls Get a directory listing to see the separate. (i.e., no linking is done!) unix> gcc -c main.c The (-c) argument configures GCC to only perform the first three steps of the process: pre-processing, compiling, and assembly. Thus, when doing C programming, the important thing to remember is that each file is compiled by itself, and then each resulting object file is linked together along with libraries (for features like printf()) to produce a single executable program.Ĭompile each.
These are all implemented by separate tools and, except for the final linking stage, are done independently for each source code (.c) file. The simple GCC command ("compile my source code into a program") actually involves several discrete stages of processing: preprocessing, compiling, assembly, and linking. Let's peel back the covers of what GCC is actually doing here. You should see a demo program consisting of two source code files ( main.c and file2.c) and two header files ( main.h and file2.h).Ĭompile them all using a single GCC command, and run the resulting program. Get a directory listing to see what files are present. Next, enter the "part2" directory of "lab02" in your private repository. This tells GCC to take the hello.c input file and preprocess+compile+assemble+link it into an executable file with the name hello_program.Ĭongrats, you're now an expert! Looks pretty easy, right? Lab Part 2 - Toolchain / Multiple Source Files
(GCC stands for the "GNU Project C and C++ Compiler") unix> gcc hello.c -o hello_program Printf(" Sawubona Mhlaba!\n") /* Zulu for "hello, world" */Ĭompile it and run using GCC, an open-source compiler. Use gedit to edit the file hello.c: unix> gedit hello.c &Įnter the following "Hello World" program written in the C programming language, and save it when finished. In time, I will give you a glimpse into the vim editor, but let us use gedit for now. Launch your favorite Linux text editor - gedit is the default for this class. Now, enter the subdirectory for lab02/part1: unix> cd part1 Copy all those directories to " ~/2022_spring_ecpe170/lab02" It has three folders: part1, part2, and part3. Hence, you will have the empty directory: " ~/2022_spring_ecpe170/lab02". For lab02 you need to make its folder inside 2022_spring_ecpe170. This is the folder to keep all your labs for ECPE 170. Lab Part 1 - Compiler Basicsįirst, ensure you are in your personal repository. Make a folder inside your ~ as 2022_spring_ecpe170. Log onto Linux and open a command prompt.
Please download the zip file lab02.zip and extract it to see all parts (part1, part2, and part3) To begin this lab, start by obtaining the necessary boilerplate code. This lab will give you hands-on experience with the C programming language, the development toolchain (pre-processor, compiler, assembler, linker), and automating the compilation process using Makefiles.