Firefly Open Source Community

   Login   |   Register   |
New_Topic
Print Previous Topic Next Topic

Using GCC compile your code in Linux environment

10

Credits

0

Prestige

0

Contribution

new registration

Rank: 1

Credits
10

Using GCC compile your code in Linux environment

Posted at 12/19/2014 15:09:21      View:6267 | Replies:7        Print      Only Author   [Copy Link] 1#

As a beginner on programming, it’s better to use GCC to compile the program. Then you can know more about the

process of a program compile and link. I advice you to using vi/vim to edit the code.

GCC provide so many command option. However, you don’t have to learn all the option. As a beginner, you only need

to be familiar with several option. Then you can begin to write your program.

1. Common compiler command options

For example, the source file is test.c

(1). Compile and link with no option

  1. <font color="rgb(88, 88, 88)"><font face="Arial, Helvetica, sans-serif"><font style="font-size: 13px">#gcc test.c</font></font></font>
Copy the code


Function: pretreatment, assemble, compile and link to an executable program.There is no output specified. “a.out” is

the default output file. After compiling successfully ,there will be a new file “a.out” in the current path. You can execute

the command to run the program.

  1. <font color="rgb(88, 88, 88)"><font face="Arial, Helvetica, sans-serif"><font style="font-size: 13px">#./a.out</font></font></font>
Copy the code

(2). Option -o

  1. <font color="rgb(88, 88, 88)"><font face="Arial, Helvetica, sans-serif"><font style="font-size: 13px">#gcc test.c -o test</font></font></font>
Copy the code


Function: pretreatment, assemble, compile and link to an executable program. The “-o” option is used to specify

the output file. After compiling successfully , there will be a new file “test” in the current path. You can execute the

command to run the program.

  1. #./test
Copy the code

(3). Option -E

  1. #gcc -E test.c -o test.i
Copy the code

Fuction: pretreatment the “test.c” and assemble to “test.s”.


(5). Option -c

  1. #gcc -c test.s
Copy the code

Function: compile the “test.s” to “test.o”


(6). Option -o

  1. #gcc test.o -o test
Copy the code

Function: link “test.o” to “test”


(7). Option -O

  1. #gcc -O1 test.c -o test
Copy the code

Using the level 1 to compile the file. The levels are 1 to 3. Higher level with


(8). Compile the C++ program file using the std libs..

  1. #gcc test.cpp -o test -lstdc++
Copy the code

Function: compile the test.cpp and link to test execute file. -lstdc++ means the std libs.


2. Compile more than one files.

If you want to compile more than one files. You can do it as the follow ways:

For example, you are going to compile these two files test.c and testfun.c

(1). Compile all files at once.

  1. #gcc testfunc.c test.c -o test
Copy the code

Function: compile the testfun.c and test.c, link to an executive file.


(2). Compile the files respectively and link them to an output file.

  1. #gcc -c testfun.c //compile testfun.c to testfun.o
  2. #gcc -c test.c //compile test.c to test.o
  3. #gcc -o testfun.o test.o -o test // link testfun.o and test.o to test file.
Copy the code

Comparing the two ways above, the fist way need to compile all files at every compile. The second way just compile

the files that have been modified, so it don’t have to compile all files at every compile.


Reply

Use props Report

2

Credits

0

Prestige

0

Contribution

new registration

Rank: 1

Credits
2
Posted at 12/27/2014 17:16:51        Only Author  2#

"test" is a bad name for an application on *nix, since there is mostly already a program called "test"-


Reply

Use props Report

133

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
133
Posted at 1/13/2026 17:04:23        Only Author  3#
Your article is absolutely brilliant, thank you for sharing! Wishing you success! Here are the free Reliable Data-Con-101 test guide materials resources for you!
Reply

Use props Report

133

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
133
Posted at 1/30/2026 08:05:41        Only Author  4#
I’m really grateful for your article, it left an indelible mark. This GH-100 latest visual cert test helped me move up in my career and get a raise. Now it’s free to all. Wishing you success in your career journey!
Reply

Use props Report

134

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
134
Posted at 2/1/2026 18:23:32        Only Author  5#
Your article is magnificent, thank you for sharing it with us! This is the Top AP-215 questions exam that led to my promotion and salary increase, and it's free today for all. Best of luck on your career path!
Reply

Use props Report

131

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
131
Posted at 2/3/2026 03:02:45        Only Author  6#
I sincerely thank you for sharing this heart-shaking article! This Valid MB-820 test materials helped me get promoted and earn more. Now, it’s free for everyone. Wishing you all success in reaching your promotions!
Reply

Use props Report

136

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
136
Posted at 2/7/2026 03:54:49        Only Author  7#
Such an inspiring read, thank you for sharing this! Free Best C_P2WAB_2507 vce study resources available for all—good luck!
Reply

Use props Report

135

Credits

0

Prestige

0

Contribution

registered members

Rank: 2

Credits
135
Posted at 7 day before        Only Author  8#
Itexamdump의 ISA 인증 ISA-IEC-62443시험덤프공부자료는 pdf버전과 소프트웨어버전 두가지 버전으로 제공되는데 ISA 인증 ISA-IEC-62443실제시험예상문제가 포함되어있습니다.덤프의 예상문제는 ISA 인증 ISA-IEC-62443실제시험의 대부분 문제를 적중하여 높은 통과율과 점유율을 자랑하고 있습니다. Itexamdump의 ISA 인증 ISA-IEC-62443덤프를 선택하시면 IT자격증 취득에 더할것 없는 힘이 될것입니다.
Reply

Use props Report

You need to log in before you can reply Login | Register

This forum Credits Rules

Quick Reply Back to top Back to list