Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-14-2004, 05:27 PM   #1
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Really world-wide variable

I made a server that uses MySQL as database and I guess that it wouldn't be good to connect again, and again, and again whenever I need to get data. The main-function uses threads to "split up" whenever it gets a connection but I thought it would be best to have only one connection to the database throughout the whole server.

So that's why the connection should be directly when the server starts up and the connection would be closed when the server dies.

Here is how I connects to the server:
mysqlcppapi::Connection con;
After this I use the "con" variable to create a query and execute the query and so on. So all other files (C++ source code files) just need the variable "con".

How should I design main to make the variable able to all the other files that need to access the database?
Old 12-14-2004, 07:10 PM   #2
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
use extern


in the header file put
extern mysqlcppapi::Connection con

in an implementation file call a constructor (you need to compile this!)
mysqlcppapi::Connection con;
mysqlcppapi::Connection con(/* ctor args */)

include the header file in any other files that need access to 'con'

perform any extra initialisation in some initialisation function called before anything else in main to setup the object, open connections etc.

If you get it wrong you will get linker errors, giving you stuff like:
multiple definitions of con

You can re-declare the variable in other files where it is needed with:
extern mysqlcppapi::Connection con

but the important part is that there is only one definition (and in c++ declaration is definition, unless you have that extern there).

Last edited by dakensta; 12-14-2004 at 07:23 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Developer Wanted - World Wide davidinv Linux - General 1 11-28-2005 02:23 PM
Linux Developer Wanted - World Wide davidinv Linux - Newbie 3 11-28-2005 12:35 PM
PERCENTAGE OF LINUX(and other non wintelOS) used WORLD-WIDE?? chilibowl General 6 03-02-2003 03:32 AM
A Linux World Wide Calendar Event? zLinuxz LQ Suggestions & Feedback 2 07-28-2002 09:33 PM
ftp from LAN to big wide world acid_kewpie Linux - Networking 2 11-14-2001 05:05 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration