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

  • Thread starter DrKareem
  • Start date
  • Tags
    Ubuntu
In summary, the person is asking for help compiling and running code in c language and does not know how to do it. They if they have a good tutorial for c they would be happy to post it.
  • #1
DrKareem
101
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
  • #2
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)
 
  • #4
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?
 
  • #5
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 '.'
 
  • #6
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:
 
  • #7
. 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
 

1. How do I install and set up C on Ubuntu?

To install and set up C on Ubuntu, you can use the command sudo apt install build-essential. This will install all the necessary tools and libraries for C development on Ubuntu.

2. How do I compile and run a C program on Ubuntu?

To compile a C program on Ubuntu, you can use the command gcc -o output_file input_file.c. This will generate an executable file with the name specified in the -o flag. To run the program, use the command ./output_file.

3. How do I debug a C program on Ubuntu?

To debug a C program on Ubuntu, you can use the gdb debugger. Compile your program with the -g flag to include debugging symbols, and then use the command gdb executable_file to start the debugger. You can then use various commands to step through your code and analyze any errors or bugs.

4. Can I use an IDE for C development on Ubuntu?

Yes, there are several IDEs available for C development on Ubuntu, such as Eclipse, Code::Blocks, and NetBeans. These IDEs provide a user-friendly interface and features like debugging and code completion to make C development easier.

5. How do I use external libraries in my C program on Ubuntu?

To use external libraries in your C program on Ubuntu, you need to install the necessary libraries using the command sudo apt install library-name-dev. Then, in your program, you can include the library using the #include directive and link it during compilation using the -l flag.

Similar threads

Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Computing and Technology
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top