0

I'm reasonably new to C++ but have just inherited a set of 20+ C++ solutions that work together to create an application suite. I have a class held in a singular .cpp file that can be used to connect and communicate with an SQL database, and I want to make all of the solutions be able to use this class.

I have put the sql.cpp file in a directory called "Shared" that already existed. But where in each project do I include the file? I have tried including it in the stdafx file but that caused endless compiler errors. I have tried including it in each individual .cpp file that uses it within the project but as soon as I include it in more than one I get "already defined in obj" errors. I have also tried creating a separate .h file to instantiate the class and including that, but it seems to not find the .cpp file as I get unresolved external errors.

I hope I have explained that well enough - could anyone shed some light on this subject?

Thanks!

4
  • Please read about declarations and definitions and have a header (h) and source file (cpp). Also, get a simple sample project you can study.
    – user2249683
    Dec 18, 2013 at 9:46
  • Yeah I understand all about declarations and definitions and the difference between h and cpp files. Which is why I can't understand why what I have isn't working. The bit I don't understand is the difference created by having the h and cpp files in a Shared directory rather than the project directory itself, as I can only think that is why this is not working.
    – Raiden616
    Dec 18, 2013 at 9:51
  • You should add .cpp file to project so it gets build and included in linking. Tell us what tools you are using? (Visual studio). The unresolved external errors means that the .cpp file was not linked to ( and probably not built. )
    – UldisK
    Dec 18, 2013 at 9:53
  • Okay if I add the cpp file to the project though, would I not have a duplicate cpp file in every single project that I would need to update every time I change something in it?
    – Raiden616
    Dec 18, 2013 at 9:56

1 Answer 1

0

You don't include the cpp file.

First, take the functions prototype and create header file

if your Sql.cpp code is

bool Open(char* name)
{
 //do something
}
void Close(char* name)
{
 //do something else
}

your Sql.h header should be: (you put only functions you want to call from outside the file)

#pragma once
bool Open(char* name);
void Close(char* name);

Second, I assume you better make a library (static library is the simplest, but better a shared or DLL depending your OS) and link to it from all the projects need it.

you can use the object file as well as static library if it not depends on anything else.

EDIT: on Windows DLL:

On windows Static lib

how to build static library in linux

how to build shared library in linux

7
  • Right okay that makes sense. Is the shared DLL created as a separate project? Is it simple to link from the projects that need it?
    – Raiden616
    Dec 18, 2013 at 9:55
  • The answer depend on which OS you are using.
    – SHR
    Dec 18, 2013 at 9:57
  • I see, my apologies. I am using Visual Studio 2010 on Windows 7 as my development machine.
    – Raiden616
    Dec 18, 2013 at 9:58
  • See my edit for Windows DLL and Static lib. I think you should start with static lib. for DLL you should do more modifications.
    – SHR
    Dec 18, 2013 at 10:18
  • Okay thank you - I am using MFC in the SQL class so would I need to do anything differently (other than including the MFC libs of course)
    – Raiden616
    Dec 18, 2013 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.