PDA

View Full Version : newbie: steps to create and compile c program in SUSE



gemini_shooter
05-21-2005, 10:55 AM
Hi !

I am a newbie and have been working in windows XP and bloodshed dev-c++ compiler b4, but recently I have made the decision and managed to switch to linux. I started by using SUSE 9.1 personal linux on Vmware workstation.

I apologize for my dumbness, but I have no clue to create and compile a c program in SUSE. Can anyone please give me a few steps on how to do that.


I opened up something called root shell and wrote the following command

" cpp pathname/filename.c"

I got a message saying cannot find file stdio.h. I am tottally lost about this,can any please give me the right way to to write a program and compile it. There is also something called kwrite and notepad. vi. I have no idea which to use to create a program.

Any help would be appreciated !!!

MathFan
05-21-2005, 01:47 PM
First thing first: programming in linux is similar to programming in windows. So the whole idea is that you create a file with some code in it and then feed it to the compiler, so that it can create an "exe"-file.

To create and edit the file with C code you can use anything you want - kwrite, vim (or vi), emacs, kate etc. All of these are simple (more or less) editor programs similar to notepad in win32.

Then we have the compiler part. The best choice is the GNU C Compiler. It must be preinstalled on the system. So how do you use it? Follow these steps:

1. Create a .c file in some directory, say /home/your_username/c/
2. Make some coding and save the file in the newely created dirctory
3. Then go to the console an browse to the folder where the .c file is placed. You can do this by typing:

cd /home/your_username/c/
And then pressing Enter.
4. Now, let's compile the program. Type:

gcc name_of_your_file.c -o name_of_exe_file
Press Enter.
5. Now the program is compiled and you can execute your program by:

./name_of_exe_file

Many people find it convinient using emacs as a C editor. But this editor is rather complex in use, so I don't know if it is the right choice for you yet.

You can also use KDevelop. It's similar to Dev-C++ (and others like VC++, BCB etc.), and has a nice front-end so you do not need to compile your project from console. If you can, use KDevelop.

MathFan
05-21-2005, 01:59 PM
I got a message saying cannot find file stdio.h.
That's rather strange though. stdio.h should have been present on your computer, to be more exact - it should be in /usr/include/ directory. That it's missing means that probably the "glibc" library is not installed. Try to install it using SUSE's package manager

gemini_shooter
05-22-2005, 02:08 AM
thank for the reply MathFan.

Here are the problems i faced , I did exactly as you said,

first error : it says gcc : no such command, so I tried cpp
second error : it says stdio.h : no such file or directory

I am running linux on Vmware and I downloaded SUSE 9.1 personal from the website "www.linuxiso.org",

Two questions:
I presume I can download SUSE package manager from SUSE's website, but Since I am running linux through Vmware do you have any idea how to install this package on it ????

MathFan
05-22-2005, 03:02 AM
Is gcc missing?? Hmmm... I thought it was a default for most linux dists. You will probably need to downlod and install it as well.

That about the package manager. I think it is preinstalled. I'm not familiar with SUSE, but I think it uses the so-called rpm-packages. To install a package, you should first of all find it somewhere, for example you can search for rpm-s here (http://rpm.pbone.net/) or just use google. When you have downloaded a package, you should get root, that is by typing:

su
Then type in your password and browse to the download location of the rpm-file. Then type:

rpm -ivh name_of_package.rpm
You will probably get some dependecy errors, like: "package some_package has to be installed". You must satisfy these dependencies by downloading and installing all needed packages.

Another thing is: Why did you choose SUSE? If you ask me Mandrake or Fedora are a better choice for a beginner. They are easy to install - and the gcc, glibc and others are present on the installation cd-s, so you won't have to look for them elsewhere. When installing Mdk Or Fedora, you will be asked if you want to install development tools. If you choose that option, everything will be installed for you.

By the way: are you sure that the gcc and glibs packages are not on your SUSE cdrom-s (or dvd-s)?

gemini_shooter
05-22-2005, 09:28 AM
I dwonloaded fedora core 3 but after all these tries I keep ketting media check failure.

I have a pretty slow connection so its hard for me to download, so the next I saw was suse which was only one CD so I went ahead and downloaded it. I want to download mandrake but its 3 CD's it will take me some time to get it, so I was wondering if I could get aways with SUSE.

Looks like not so I guess I will have to go with MANDRAKE, but it will take me while to get it, I am downloading from "www.linuxiso.org"

MathFan
05-22-2005, 10:49 AM
The problem is that SUSE is not a free OS like Mandrake and Fedora. A full version of it will cost you money, while the cd you downloaded is only a kind of a preview. It doesn't include the usual things other linux distros have as default.

Yes, mandrake is a good choice. Be sure though to choose "development packages/software" in the "Package Selection" step during the installation of mdk.

gemini_shooter
05-22-2005, 10:22 PM
I ran yast2 on SUSE 9.1 and these are the packages I could find which puzzles me even more now :

Package Summary Size Version
cpp The GCC preprocessor 3.6MB 3.3.3-41
libgcc C compiler Runtime library 39.2kB 3.3.3-41

Does this mean I already have gcc installed but why can't I run it then ?

MathFan
05-23-2005, 07:50 AM
cpp is only a preprocessor - it's not the compiler (but, sure, it's a part of the GNU C and as far as I know is needed for gcc to run). Also libgcc is needed for the gcc to run. So that's a good start. Now, the only thing you need to do is to go here (http://rpm.pbone.net/index.php3/stat/4/idpl/1694870/com/gcc-3.3.3-43.31.i586.rpm.html) and download the gcc-3.3.3 package and install it. Then try to compile your files.

If it doesn't work, tell me.

MathFan
05-23-2005, 07:55 AM
Take a look at the page I gave you in the previous post. Note that under the download mirror links, it lists the required packages. You MUST have all of these to get gcc to install. So if they are not present on your system, you will have fetch and install them too.

edit: You must make sure that the version of the packages match. Like: if you have gcc-3.3.3, you must also have libgcc-3.3.3. Another thing is that since you have SUSE, you must look for rpm-packages for SUSE, and not for, say mandrake. Sometimes packages form other systems may work on SUSE, but it is not advised to mix them like this.

edit2: One more thing: glibc-devel is NOT the same as glibc!

KntTwist
05-23-2005, 09:14 AM
I use SuSE 9.2, and it was also my first intro to Linux. For coding in C, I use Kate, 'cause it has a handly little terminal emulator within.

But as for GCC, try http://gcc.gnu.org/ It may help you figure out a few things.

dwks
06-20-2005, 04:16 PM
Now the program is compiled and you can execute your program by:


./name_of_exe_file



Thank you very much! I could compile C programs but not run them. All because I forgot the "./" . . . .

dwk

kermit
06-22-2005, 06:35 PM
cpp is only a preprocessor - it's not the compiler (but, sure, it's a part of the GNU C and as far as I know is needed for gcc to run). Also libgcc is needed for the gcc to run. So that's a good start. Now, the only thing you need to do is to go here (http://rpm.pbone.net/index.php3/stat/4/idpl/1694870/com/gcc-3.3.3-43.31.i586.rpm.html) and download the gcc-3.3.3 package and install it. Then try to compile your files.

If it doesn't work, tell me.

A friend of mine had the same problem with SuSE 9.1.. we were scratching our heads for a minute, wondering why a Linux distro would not have a C compiler, but anyway.. We fixed the problem with YAST - and I would recommend this as a solution for the O.P.

YAST works nicely as you should have few dependency problems.

@ gemini_shooter:

Open Yast up to and have another look for gcc - it should show up there.