• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Beginners Guide to compiling in Linux

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> UNIX // GNU/Linux

View previous topic :: View next topic  
Author Message
GSecur
Trusted SF Member
Trusted SF Member


Joined: 30 Sep 2002
Posts: 16777215


Offline

PostPosted: Sun Mar 02, 2003 7:41 pm    Post subject: Beginners Guide to compiling in Linux Reply with quote

OK, Ive seen a lot of questions lately about compiling programs in Linux so I decided to write this for you guys. I hope it helps out. Letˇ¦s start out with the basics.

Vocabulary
Source: Youve probably seen this before Download Source In Linux (or unix) you will most likely be dealing with source code. This is actually the raw instructions that are written by the programmer. You can not run these! So dont try Smile. You need to first compile them (well get to that later)

Binaries: Binaries are code that has been compiled. If you are downloading binaries you will need to insure that the binary was compiled on the same distro and chipset you are using. Most Linux users will always say it is better to compile everything from the source. In most cases this is true. But, if you are a beginner try to find binaries:-)

Compiler: This is the actual program that interprets the source into a binary format. Logically, your compiler must be for the language the source was written. The vast majority of the time it will be a C compiler, the most common is gcc.

Files you will need:
A compiler (duh) ok for this tutorial we are going to use gcc.

Redhat 8.0
ftp://rpmfind.net/linux/redhat/8.0/en/os/i386/RedHat/RPMS/gcc-3.2-7.i386.rpm
Source
ftp://rpmfind.net/linux/redhat/8.0/en/os/i386/SRPMS/gcc-3.2-7.src.rpm

Mandrake
ftp://rpmfind.net/linux/Mandrake/9.0/i586/Mandrake/RPMS/gcc-3.2-1mdk.i586.rpm
Source
ftp://rpmfind.net/linux/Mandrake/9.0/SRPMS/gcc-3.2-1mdk.src.rpm

Debian
< if you are using Debian you should know how to compile Smile >

But it's apt-get install gcc if you want it, or select development modules from tasksel.


Some more basics:

Another thing a lot of beginners get confused with is the difference between a script and the source for a program. Scripts like pearl, PHP, ASP, and VBScript are interpreted at runtime. A compiled program is interpreted when you actually compile it, and then simply run. This means respective to their complexity compiled programs run faster than scripts.

Most software for Linux and other open source platforms are distributed as source code, usually a compressed archive commonly known as the tarball. This just means the file is in a compressed format, pretty much the same thing as a ZIP file.

Usually before you compile any programs, you should be logged in is a root user. You can do this by logging in as root or by typing su at the command line.

Another common mistake people make is to not have the required libraries. Libraries are shared code, that a program needs to run. For example: most programs that use extensive references to a certain type of protocol, such as SNMP will use a common library written by another programmer. Always check the readme file to see what libraries a program might need.

OK, the start out with a simple program we could write:

Code:
Int main(){
   Printf(ˇ§Hello, worldˇ¨);
}


All this program does, is print hello world, easy enough, let's save it as test.c. Now to compile it just type:

Code:
   gcc test.c


Now we should have a working binary program called a.out by default. If you want to give it a different name just type:

Code:
gcc test.c -o test1


The will output the file as test1.

Now remember how we talked about libraries, well libraries are included in source by the following:

Code:
#include <SDL/SDL.h>


All includes will be displayed at the top of the source code to be within good programming principles. Now, when we compile our program that contains includes we need to tell GCC where the library is located, so that a permanent link can be made within the program. We do this by the following:

Code:
gcc test.c -o test1 -L/usr/lib -ISDL


OK, here's quick English rundown of what we just did. We told GCC to compile our source test.c and output it as test1 and told it that the library SDL was located in /usr/Lib.

Now I hope this helped you guys out a little bit. The final thing I'm going to cover, is a lot of people see in the install files of a lot of programs that they need to run ./configure. Now when I was just beginning I used to get frustrated, because I would type that at the command line, and I would get file not found. The trouble you're having is you are not in the correct shell of Linux. To fix this just type:

Code:
sh ./configure


OK, that just about wraps it up. I hope I was able to answer most of your questions, if you have any more just ask me. I'll do my best to answer any questions.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
White Scorpion
Just Arrived
Just Arrived


Joined: 19 Sep 2003
Posts: 5
Location: The Netherlands

Offline

PostPosted: Thu Oct 16, 2003 8:41 pm    Post subject: Reply with quote

ok, thanks for the tutorial,

but i got another question (probabely a simple one for you) :

i'm trying to install a new kernel in my version of RH7.1 my currnet kernel is version 2.4.2-2 and i'm trying to install linux2.6.0-test1 which i've downloaded from some site.

Now for the problem, should i just copy the new kernel to the same folder as the old kernel is in, and then delete the old kernel? can't imagine that, for the most part i've found on the internet that you should change the directory of the kernel to the new one somewhere, don't know where...

any suggestions??
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Viggel
Just Arrived
Just Arrived


Joined: 01 Oct 2003
Posts: 0


Offline

PostPosted: Thu Oct 16, 2003 9:39 pm    Post subject: Reply with quote

...

Last edited by Viggel on Mon Oct 05, 2009 9:57 pm; edited 1 time in total
Back to top
View user's profile Send private message
yoshi_yaki
Just Arrived
Just Arrived


Joined: 04 Nov 2005
Posts: 0


Offline

PostPosted: Sat Nov 05, 2005 2:21 pm    Post subject: Reply with quote

Viggel wrote:
Nice tut Gsecure, you might want to add that if they want to run test1 if they're in that directory where it is to type ./test1 and you can see the output of the file but that's more programming now.

Lepri
I take it you done make xconfig (<=loaded up the old .config file if necessary)
make dep bzImage modules modules_install



If you're in /usr/src then the folder where you're new kernel is you can type cp arch/i386/boot/bzImage /boot/vmlunix-2.6.0-test1
then type cp System.map /boot/System.map-2.6.0-test1
Then depending on bootloader edit the bootloader .conf file and add the new kernel and run the bootloader in the shell to update it. I wouldn't delete any kernel especially as it's a test one, just choose the one you want to run at boot time.

That's how I would do it anyway and have done for a while and it works amazingly ,any probs just ask.

Viggel

Isn't make dep depreciated? I usually just make;make modules;make modules_install. During make, the bzImage is generated automatically.
Back to top
View user's profile Send private message
phate
Just Arrived
Just Arrived


Joined: 09 Feb 2006
Posts: 0


Offline

PostPosted: Thu Feb 09, 2006 9:32 pm    Post subject: Reply with quote

Thats all very well and a good tutorial but wat bout beginners... you have not saidf where to put the codes in! ie. beginners question... where do i put codes in??? notepad???
Back to top
View user's profile Send private message
cryolord
Just Arrived
Just Arrived


Joined: 22 Feb 2006
Posts: 0
Location: /home/cryolord

Offline

PostPosted: Wed Feb 22, 2006 11:19 pm    Post subject: Reply with quote

haven't you noticed that this is all about linux? what notepad? but yes, you write source code in some text editor, kate, vim, emacs or whatever you prefer...
Back to top
View user's profile Send private message
wickerandvine
New Member
New Member


Joined: 20 Oct 2006
Posts: 25
Location: alang-alang Mandaue

Offline

PostPosted: Fri Dec 15, 2006 3:34 am    Post subject: Reply with quote

In the linux world there is no such thing as a notepad. It only exist on windows. not unless you use winex to run windows programs under linux.
Back to top
View user's profile Send private message
Baraba
Just Arrived
Just Arrived


Joined: 29 May 2008
Posts: 0


Offline

PostPosted: Fri May 30, 2008 1:28 am    Post subject: Reply with quote

Can anybody tell me something about shell scripts?
Can I compile c++ codes in VI editor?

Moderator note: edited to remove commercial signature - capi
Back to top
View user's profile Send private message
BluePass
Just Arrived
Just Arrived


Joined: 23 Mar 2008
Posts: 0


Offline

PostPosted: Thu Jun 05, 2008 1:38 am    Post subject: Reply with quote

You cannot compile within VI -- at least not that I know of. VI is a text editor not an IDE.

What exactly do you want to know about shell scripts?
Back to top
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Thu Jun 05, 2008 3:45 am    Post subject: Reply with quote

Baraba wrote:
Can anybody tell me something about shell scripts?

42.

For a more specific answer, try asking a question.

Baraba wrote:
Can I compile c++ codes in VI editor?

I don't know what this VI editor is, but the vi editor lets you run arbitrary external programs with the :! command.
Back to top
View user's profile Send private message
Baraba
Just Arrived
Just Arrived


Joined: 29 May 2008
Posts: 0


Offline

PostPosted: Sun Jun 08, 2008 12:53 am    Post subject: Reply with quote

You never heard for gcc C compiler and gdb C debuger in LINUX?
Pffff

Moderator note: lose the signature spam already - capi
Back to top
View user's profile Send private message
BluePass
Just Arrived
Just Arrived


Joined: 23 Mar 2008
Posts: 0


Offline

PostPosted: Sun Jun 08, 2008 2:11 am    Post subject: Reply with quote

Baraba, how many years of experience on Linux do you have? And what distribution do you use?

No, I've never heard of gcc and gdb. That must be some tools leet hackers use. </sarcasm>

Also, gcc is not only a C compiler, and gdb is not only a C debugger.

I'm sorry, but I hate arrogance.
Back to top
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Sun Jun 08, 2008 4:27 pm    Post subject: Reply with quote

Baraba wrote:
You never heard for gcc C compiler and gdb C debuger in LINUX?
Pffff

If you're going to throw flame bait around, at least get your names right.

Linux is not an acronym, it's just Linux; not LINUX. And it's a kernel, so there's no "gcc C compiler" in it. You probably meant to say GNU/Linux.

GCC, on the other hand, is an acronym (points for guessing what it stands for). And as BluePass noted, it's not just a C compiler (nor is it even just a compiler in the first place).

If you want to have a positive and productive conversation, we can do that, and we can all have fun and talk. If, on the other hand, you just want to throw unwarranted flame bait around and spam your commercial signature, we can do that too - the "delete post" button comes very handy in those cases.
Back to top
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> UNIX // GNU/Linux All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Looking for more Windows Networking info?

Sign up to the WindowsNetworking.com Monthly Newsletter, written by Enterprise Security MVP Deb Shinder, containing news, the hottest tips, Networking links of the month and much more. Subscribe today and don't miss a thing!
View a sample newsletter.

Become a WindowsNetworking.com member!

Discuss your Windows Networking issues with thousands of other Windows Newtorking experts. Click here to join!

Community Area

Log in | Register

Readers' Choice

Which is your preferred data recovery solution?

Follow TechGenix on Twitter