ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to get a first program going in C in Ubuntu 8.04.
It's the usual "hello world" stuff the books introduce you to but there's a few things I can't understand at this stage.
The book mentions a /home/MyPrograms directory. I can't seem to find this at all? And should my program be saved in a text document labeled (name).c
eg. hello.c?
From what I can see the final commands are executed in the terminal.
You would want to make a directory called /home/MyPrograms. Yes, you should name your source code files with the .c extension.
To compile a helloworld.c file eg.
Code:
include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
You would want to run this command to compile it:
Code:
gcc helloworld.c -o helloworld
That creates a program called helloworld
To run it, first make sure its executable by running
Code:
chmod +x helloworld
Then run
Code:
./helloworld
and that should output Hello World
Last edited by AceofSpades19; 02-28-2009 at 05:45 PM.
The first was to manually create a MyPrograms directory in /home/myusername, put the home code in there in a home.c text file, but all I got was
Code:
gcc: hello.c: No such file or directory
gcc: no input files
after putting in the terminal instructions gcc hello.c -o hello.exe
I then tried the
Code:
#mkdir ./MyPrograms
(as below), after deleting my manual entry (as above) but could not find the directory.
Any suggestions?
if you have a filename called home.c, you wouldn't compile it with
gcc hello.c -o hello, you would compile it with gcc home.c -o home and you do not need to put the .exe extension for a program
I wouldn't worry about the "/home/MyPrograms" stuff. Just focus on compiling your program:
Quote:
1. Create your "hello.c" text file:
Code:
#include <stdio.h>
int
main (int argc, char *argv[])
{
printf ("hello world!\n");
return 0;
}
2. Verify that the file exists (this is actually important):
Quote:
ls -l hello.c
<= If you don't see "hello.c" when you type this "ls -l" command, you're going to have problems. Post back if you have any problems up to this point
3. Compile and link the file ("gcc" does both things for you at once):
Quote:
gcc -g -Wall -o hello hello.c
<= These extra "flags" ("-g" and "-Wall") will help you debug more advanced programs you write
4. Execute:
Quote:
./hello
'Hope that helps .. PSM
PS:
Linux offers many IDE's (kdevelop and Eclipse, among many others) to simplify compiling and debugging programs, should you prefer.
OK- the good news is that I've passed stage 2 of your first post. I did notice however that the system seems to prefer it if files are kept in my own home directory and not in a new folder eg /MyPrograms.
I also noticed 2 hello files after entering "ls -l":
but in search for files, there is only 1 so this may not be significant.
However, at stage 3, when I enter
Code:
gcc -g -Wall -o hello hello.c
I get the following message:
Code:
hello.c:1:19: error: stdio.h: No such file or directory
hello.c: In function ‘main’:
hello.c:6: warning: implicit declaration of function ‘printf’
hello.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
It sounds like you're nearly home free (and apologies that you've invested this much effort - things usually go a lot quicker and smoother. Honestly!)
ANYWAY:
1. From your command prompt, type:
Quote:
dpkg -l|grep -i gcc
ii gcc 4:4.2.3-1ubuntu6 The GNU C compiler
ii gcc-4.2 4.2.3-2ubuntu7 The GNU C compiler
ii gcc-4.2-base 4.2.3-2ubuntu7 The GNU Compiler Collection (base package)
ii libgcc1 1:4.2.3-2ubuntu7 GCC support library
ii libgomp1 4.2.3-2ubuntu7 GCC OpenMP (GOMP) support library
<= I'm guessing you *have* gcc (the compiler), but you need to install the C runtime headers and libraries
2. Use the Synaptic GUI to install:
Administration, Synaptic, Search, gcc
<= Install:
gcc-4.2 (or whatever your current version is)
gcc-4.2-base (this is probably what's causing the error)
gcc-4.2-doc (optional...but helpful)
libgccl (if you're missing the .h headers, you should also make sure you have all the C runtime libraries, too)
<= Install g++
3. If you did a "File, Save as" hello.c to "/home/MyPrograms", you should be able to compile it like this:
Quote:
gcc -o hello /home/MyPrograms/hello.c
... OR ...
cd /home/MyPrograms
gcc -o hello hello.c
There's nothing "magic" about /home/MyPrograms
4. The "hello.c~" is almost certainly either a temp file (created by your text editor), or a backup file (created by your text editor)
5. For more complicated programs, obviously, you won't be typing "gcc -o hello hello.c" on a command line. You'll probably instead use tools like "make" (for batch-oriented builds from a command line), or "kdevelop" (an interactive GUI for creating and debugging programs).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.