Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C using ubuntu.

  1. Sep 24, 2006 #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.

  2. jcsd
  3. Sep 24, 2006 #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. Sep 24, 2006 #3
  5. Sep 24, 2006 #4
    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?
  6. Sep 24, 2006 #5
    Yes. That was a typo. I just forgot the '.'
  7. Sep 24, 2006 #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:
  8. Sep 26, 2006 #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

    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 dont 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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook