Compiling and Linking - tired of being confused

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Compiling and Linking - tired of being confused

Postby sooner » Fri Jun 24, 2005 7:42 pm

Guys. I need some help/advise.

I'm writing c++ internet apps for a Unix server, stuff like shopping carts, etc... that run in a browser. Most of my apps right now are ran through a browser.

My problem is this. Often, I have to compile and link other libraries etc.. to my own code. But I don't know how to do it!

So I'll struggle for hours and days trying to figure it out.

Right now I'm trying to access a mysql database with c++. I think the library to do so is already on my server, but I can't tell.

At any rate, I contacted my hosting provider and they give me a prompt response letting me know what I have to do.
viewtopic.php?t=10750
(see that post for details)

Anyway, I need to figure out what they're talking about when they refer to using -Wall and linking with a '-lmysqlclient ' switch etc..

But I can not find any good resourse on the internet that explain this in plain english!
I need to know the basics and then be taken through, step by step, of how to complie multiple files from a command line (telnet)
what order you do stuff in?
Link, then compile? Compile then link?
how do you link? Why do you link? what is linking?
What is the -a for? What is the -Wall for.
etc...
That is the sort of info I need. I dont mind taking the time to learn it myself, but I can't find the info anywhere.

Is there a book somewhere that specifically goes through this stuff for a complete newbee? A web resourse that takes a beginner through it?

And finally, where did you learn it at? College? On the job? A good book?
I'm tired of spending weeks ... and weeks ... and weeks ... sometimes to figure out one simple thing.

Sorry for whining. :? And I appreciate any help you can give.
User avatar
sooner
 
Posts: 95
Joined: Fri Feb 04, 2005 12:27 pm
Location: Georgia, United States

Postby MXP » Fri Jun 24, 2005 8:33 pm

If you're on Unix you can probably just type in

man g++

to read the documentation for g++ (or gcc if you're using that). Anyway, -Wall means show all warnings. Commands that begin with -W deal with warnings. -lmysqlclient means that the mysqlclient library is linked. To link a file use -llibrary-name where the library files have filenames like liblibrary-name.a

When you turn some source files into a program the process goes like this:
first each source file is compiled into an object file. On gcc and g++ object files end in .o, for MSVC++ object files end in .obj. Then the object files are linked into a single executable.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby sooner » Fri Jun 24, 2005 9:10 pm

Thanks Colin.

I did just finish reading the manual. And it helped.

So baically, every file you have is converted into an object file first. ( .o files)
Then the .o files are all 'linked' and compiled into one executable?

I'm guessing that a library is a group of .o files??
And then you link a library into your own code to make an exe file?

I do have this much figured out ...
Code: Select all

// mycode.c and mycode.h
g++ -c mycode.c
// this makes mycode.o

// then

g++ main.cpp mycode.o -o myprogram.exe
// makes the full executable


What I haven't quite figured out yet is the linking and all the switches and how to include a library. (or how to even compile a library).
User avatar
sooner
 
Posts: 95
Joined: Fri Feb 04, 2005 12:27 pm
Location: Georgia, United States

Postby GeekDog » Mon Jun 27, 2005 2:33 pm

Any library you use should come with instructions about how to compile it, if it isn't pre-built (which they often are). Linking, as far as I know, is done in a similar way to compiling and linking object files, you just need to use the correct flags as Colin mentioned.

You might also want to look into using a program called make. It handles the compiling and linking for you, so instead of the commands you typed above you just have to type something like "make myprogram" and it'll call g++ for you. If you have specific question let me know and I'll try to help, as I use Linux quite a bit at work (although I am no expert, so no guarantees!).
User avatar
GeekDog
 
Posts: 1160
Joined: Sun Sep 28, 2003 1:42 pm
Location: UK


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 1 guest