Thread: Multiple .cpp files

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115

    Multiple .cpp files

    I have 6 .cpp files, each containing Dialog procedures and some functions. I also connect to a SQLite database - I read location of database from config file and the main problem is...

    How to store config value in main.cpp (first file) that will be simply accessable in other .cpp files ?

    Thanks for all answers!
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Would a global variable in a header file suffice? Or you could declare extern variables defined elsewhere in your program.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    OK, I tried to put a variable in header, which is included by all .cpp files, and I got this error from linker:

    make -f Makefile.win
    g++.exe obj/functions.o obj/main.o obj/login.o obj/users.o obj/groups.o obj/cars
    .o obj/profile.o obj/nalogi_private.res -o "bin\nalogi.exe" -L"D:/Emil/devcpp/li
    b" -L"D:/Emil/devcpp/dokumenti/nalogi/headers" -L"D:/Emil/devcpp/dokumenti/nalog
    i" -mwindows ../../lib/libsqlite3.a static/md5.a ../../lib/libsqlite3_plus.a sta
    tic/CppSQLite3.a lib/sqlite3.lib -lgmon -pg -fmessage-length=0
    obj/main.o:main.cpp.bss+0x0): multiple definition of `user'
    obj/functions.o:functions.cpp.bss+0x0): first defined here
    obj/main.o:main.cpp.bss+0x20): multiple definition of `md5'
    obj/functions.o:functions.cpp.bss+0x20): first defined here
    obj/main.o:main.cpp.bss+0x5c): multiple definition of `db'
    .......
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Anybody?
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why not use a class and a static variable? or you could even just pass the instance of the class around...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the same way you'd pass around any other variable... here's an example of using a class with a static member:

    main.cpp
    Code:
    #include <iostream>
    #include "test.h"
    #include "test1.h"
    
    int main()
    {
    	testClass a;
    	a.writeVal(10);
    	std::cout<<a.readVal()<<std::endl;
    	changeVal(0);
    	std::cout<<a.readVal()<<std::endl;
    	return 0;
    }
    test.h:
    Code:
    #ifndef __TEST_CLASS__
    #define __TEST_CLASS__
    
    class testClass
    {
    	public:
    		testClass(const int in=0);
    		void writeVal(const int in);
    		int readVal();
    	private:	
    		static int val;
    };
    
    int testClass::val=0;
    testClass::testClass(const int in)
    {
    	val=in;
    }
    void testClass::writeVal(const int in)
    {
    	val=in;
    }
    int testClass::readVal()
    {
    	return val;
    }
    
    #endif
    test1.h:
    Code:
    #ifndef __TEST_FUNCTION_H__
    #define __TEST_FUNCTION_H__
    
    #include "test.h"
    
    void changeVal(const int in)
    {
    	testClass a(in);
    }
    
    #endif
    now here's how you would pass an instance of the class (I wouldn't use this in your case)

    main.cpp:
    Code:
    #include <iostream>
    #include "test.h"
    #include "test1.h"
    
    int main()
    {
    	testClass a;
    	a.writeVal(10);
    	std::cout<<a.readVal()<<std::endl;
    	changeVal(0,a);	//passing in class a by reference
    	std::cout<<a.readVal()<<std::endl;
    	return 0;
    }
    test.h:
    Code:
    #ifndef __TEST_CLASS__
    #define __TEST_CLASS__
    
    class testClass
    {
    	public:
    		testClass(const int in=0);
    		void writeVal(const int in);
    		int readVal();
    	private:	
    		int val;	//this no longer needs to be static
    };
    
    //int testClass::val=0;
    //this is no longer needed because it's not static
    testClass::testClass(const int in)
    {
    	val=in;
    }
    void testClass::writeVal(const int in)
    {
    	val=in;
    }
    int testClass::readVal()
    {
    	return val;
    }
    
    #endif
    test1.h:
    Code:
    #ifndef __TEST_FUNCTION_H__
    #define __TEST_FUNCTION_H__
    
    #include "test.h"
    
    //now that you have testClass a, you can use any of it's public methods
    //	(readVal or writeVal in this case)
    void changeVal(const int in,testClass&a)
    {
    	a.writeVal(in);
    }
    
    #endif
    edit: there's no way I imagined that post... I could have sworn I saw him ask how to pass an instance of a class around...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115

    Post

    OK. Sorry for deleting post, I thought that I will figure out myself, but I couldn't

    Here's the class definition:

    PHP Code:
    class DataAll
    {
    private:
    static const 
    charsqlite_path;
    static 
    CppSQLite3DB db;
    static 
    CMD5 md5;
    static 
    Uporabnik user;
     
    public:
    CppSQLite3DB GetDbConn();
    CMD5 GetMD5();
    const 
    charGetDbPath();
    Uporabnik GetStructUporabnik();
    bool WriteStructUporabnik(Uporabnik structure);
    }; 
    And the way how I use the class...:

    PHP Code:
    const charDataAll::sqlite_path "program.db";
    DataAll Data;
    CppSQLite3DB db Data.GetDbConn();
    const 
    charsqlite_path Data.GetDbPath();
    Uporabnik user Data.GetStructUporabnik(); 
    But I still get the same errors from linker (altough now are poiting to class variables)...
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by publikum
    OK. Sorry for deleting post, I thought that I will figure out myself, but I couldn't

    Here's the class definition:

    ...

    And the way how I use the class...:

    ...

    But I still get the same errors from linker (altough now are poiting to class variables)...
    what errors are you getting? if they're errors about not undefined references, it's probably because you forgot to define your static data members (they have to be defined outside the class body - I.E. the declaration does not count as a definition, as opposed to a regular data member, where all you need to do is declare it in the class body.
    Code:
    class DataAll
    {
    	private:
    		static const char* sqlite_path;
    		static CppSQLite3DB db;
    		static CMD5 md5;
    		static Uporabnik user;
    
    	public:
    		CppSQLite3DB GetDbConn();
    		CMD5 GetMD5();
    		const char* GetDbPath();
    		Uporabnik GetStructUporabnik();
    		bool WriteStructUporabnik(Uporabnik structure);
    };
    
    /* this is the part I was talking about */
    const char* DataAll::sqlite_path = "program.db";
    CppSQLite3DB DataAll::db=???;
    CMD5 DataAll::md5=???;
    Uporabnik DataAll::user=???;
    Last edited by major_small; 08-23-2005 at 06:46 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    OK, thanks for solution, but I still get an error from linker:

    make -f Makefile.win
    g++.exe obj/functions.o obj/main.o obj/login.o obj/users.o obj/groups.o obj/cars
    .o obj/profile.o obj/nalogi_private.res -o "bin\nalogi.exe" -L"D:/Emil/devcpp/li
    b" -L"D:/Emil/devcpp/dokumenti/nalogi/headers" -L"D:/Emil/devcpp/dokumenti/nalog
    i" -mwindows ../../lib/libsqlite3.a static/md5.a ../../lib/libsqlite3_plus.a sta
    tic/CppSQLite3.a lib/sqlite3.lib -lgmon -pg -fmessage-length=0
    obj/main.o:main.cpp.bss+0x24): multiple definition of `DataAll::user'
    obj/functions.o:functions.cpp.bss+0x0): first defined here
    obj/main.o:main.cpp.bss+0x40): multiple definition of `DataAll::md5'
    obj/functions.o:functions.cpp.bss+0x20): first defined here
    obj/main.o:main.cpp.bss+0x7c): multiple definition of `DataAll::db'
    obj/functions.o:functions.cpp.bss+0x5c): first defined here
    ........more errors like this........
    collect2: ld returned 1 exit status
    make: *** [bin/nalogi.exe] Error 1
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  10. #10
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    For your case you'd probably just want to use global variables. Just stick it in a header file that's included in all the other ones and you can use it in all of them.

    Remember, if you create your global int lalala, remember to get rid of the int lalala = ... in your files, I'm guessing that's what caused the multiple definition errors.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    so what does your code look like so far?

    I suggest NOT using global variables... ever.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Here's the code that I use in all .cpp files:

    PHP Code:
    const charDataAll::sqlite_path "program.db";
    DataAll Data;
    CppSQLite3DB db Data.GetDbConn();
    const 
    charsqlite_path Data.GetDbPath();
    Uporabnik user Data.GetStructUporabnik(); 
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what about the class declaration and it's definitions?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    you are using the sqlite .dll?

    from reading the api, the db location is a file in the application directory, unless you specify otherwise.

    you could use a conditional definition of the include, so it's only actually included once.
    that will get rid of the multiple definition errors.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  15. #15
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you have to use globals, you can group them in a class like this

    Globals.h
    Code:
    #include <string>
    
    class Globals
    {
    	public:
    		static std::string Path;
    		static int Value;
    };
    Globals.cpp:
    Code:
    #include "Globals.h"
    
    std::string Globals::Path = "initialised";
    int Globals::Value = 99;
    main.cpp:
    Code:
    #include "Globals.h"
    
    #include <string>
    #include <iostream>
    
    int main()
    {
    	std::cout << Globals::Path << std::endl;
    	std::cout << Globals::Value << std::endl;
    
    
    	Globals::Path = "c:\\test\\testfile.txt";
    	Globals::Value = 7;
    
    	std::cout << Globals::Path << std::endl;
    	std::cout << Globals::Value << std::endl;
    
        return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  2. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  3. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  4. Including multiple .cpp file in project
    By bonkey in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2002, 08:41 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM