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
Click For Summary

Discussion Overview

The discussion focuses on compiling and running C code on Ubuntu, addressing the necessary commands, tools, and potential issues for users new to Linux. Participants share their experiences and preferences regarding text editors and IDEs for C programming.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant inquires about the commands needed to compile and run C code on Ubuntu, expressing uncertainty about whether the compiler is installed.
  • Another participant suggests installing build-essentials and provides commands for compiling and running a C program using gcc, noting that Ubuntu may not come with gcc pre-installed.
  • There is mention of using the make command to build C files, along with a recommendation for the Code::Blocks IDE for C and C++ development.
  • A participant questions the command for running the compiled program, suggesting it should be "./a.out" instead of "/a.out," leading to a correction regarding a typo.
  • Further clarification is provided about the use of "./" to indicate the current working directory when executing compiled code.
  • Discussion includes various text editors and IDEs, with some participants expressing preferences for alternatives to vi, such as emacs, vim, kdevelop, and anjuta.

Areas of Agreement / Disagreement

Participants generally agree on the commands for compiling and running C code, but there is some debate regarding the best text editor or IDE to use, with no consensus on a single preferred option.

Contextual Notes

Some participants mention confusion regarding command usage and the default output file name when compiling, indicating a need for clarity on command line arguments and options available with gcc.

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
 

Similar threads

Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
5K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K