Learn How to Compile and Run C Code on Ubuntu | Helpful Tips and Tutorials

  • Thread starter Thread starter DrKareem
  • Start date Start date
  • Tags Tags
    Ubuntu
AI Thread Summary
To compile and run C programs on Ubuntu, first ensure that the build-essential package is installed using the command "sudo apt-get install build-essential," as GCC is not included by default in some installations. To compile a C file named "something.c," use the command "gcc something.c," which generates an executable named "a.out." To run the program, execute it with "./a.out." Alternatively, you can specify a different output file name with "gcc something.c -o myprogram" and run it using "./myprogram." For editing code, users are encouraged to explore various text editors beyond VI, such as Code::Blocks, Anjuta, or graphical editors, to enhance their coding experience. Additional resources and tutorials for learning C programming can be found on forums and dedicated websites.
DrKareem
Messages
101
Reaction score
1
Hello everyone. I know in ubuntu, and any other linux or Unix based systems perhaps, one would have to write the code in VI, save it, and then compile and run it. My numerical analysis teacher gave us a few assignments and he wants them to be done using the c language. The thing is I don't know the commands to compile and run c languages using my Linux (ubuntu). I don't even know if I have the compiler installed or not. So I was just wandering if any of you can help me out with this one.

Oh, if you also have some good c tutorials, feel free to post them here too. I'm a bit proficient in C++, but the projects would have to be in C, so i'd have to learn it.

Cheers.
 
Technology news on Phys.org
Since you're using ubuntu, you'll have to make sure you have installed build-essentials, sudo apt-get install build-essentials. For some reason ubuntu is like the only distro I know of that doesn't already come with gcc installed. I'm a gentoo user. Anyway, say you have your program something.c, just "gcc something.c", and to run it, do "/a.out". There are other ways of course, "gcc something.c -o something", "./something". I think this is what you're asking. If not, sorry.

Oh and BTW, VI sucks. (use/learn emacs)
 
Tony11235 said:
Anyway, say you have your program something.c, just "gcc something.c", and to run it, do "/a.out".
Please correct me if I'm wrong, I'm new to Linux, but shouldn't the command be "./a.out"? Or does the command vary with distro?
 
neutrino said:
Please correct me if I'm wrong, I'm new to Linux, but shouldn't the command be "./a.out"? Or does the command vary with distro?

Yes. That was a typo. I just forgot the '.'
 
Thanks. I've been doing some very basic c programs in FC, and I was told to do it with '.', and hence the confusion. :smile:
 
. refers to current working directory.

When you compile your code without giving the object file name,

i.e gcc code.c

the default object code is dumped into a file called a.out in your current working directory.

Therefore for execution of that code, you give the "path" as ./a.out. Say if you current working directory is /home/foo then you could also execute it as
$/home/foo/a.out

To dump the object code into another file, do
gcc code.c -o objcode

This objcode will be created in the current working directory. You can also specify the entire path to the place where you want your objcode to be placed.

gcc code.c -o /home/foo/compiled/objcode

Just man gcc to find out more on command line arguments that you can give to gcc like -l -I switches. You will require -l for using math lib.

Editors :
You don't have to use vi for writing code. You can use any text editor to write code. If you are uncomfortable with vi, you can use any graphical text editor that you may have. I write my codes normally in vim, but for huge projects i normally prefer kdevelop or anjuta.

You can try anjuta, you might find it more friendlier than any other IDE at the moment.

-- AI
 
Back
Top