1

I have two Visual C++ projects (in Visual studio 2008), one of them has an user interface which is developed with qt 4.6.2 (and the project is in 32 bit configuration) and the other one is a console application (with 64 bit configuration) both are in same solution.

So I need to use some classes of console application in the other one.

Let's say console app has one class like that:

class a
{
public:
   void read();
   void write(); 
}

so I'm adding this project's folder to additional dependencies of the other project which needs to use class a, and trying to use it like:

#include "a.h"

a myClass;
myClass.write();

when I compile the solution it gives Error LNK2019: Unresolved External Symbol.

So am I doing something wrong or is it because of different configuration of the projects?

Thanks.

Vecihi
  • 261
  • 4
  • 12
  • Besides the includes, are you linking against the library - http://stackoverflow.com/a/12574400/673730 ? – Luchian Grigore Feb 20 '14 at 20:13
  • Thanks for the answer. I don't have lib files, only headers for the first project. So do I need to add anything for the linker as well? – Vecihi Feb 20 '14 at 20:17

3 Answers3

0

You need to include one of the following in your project in order to link against it:

  1. a.cpp, or
  2. A library (*.lib) which compiled a.cpp (this could also be used in combination with a dll)
Peter R
  • 2,865
  • 18
  • 19
  • I don't have lib or dll in first project. Only header files and obviously the implementation files (cpp's). What do you mean by linking cpp? – Vecihi Feb 20 '14 at 20:20
  • Then you need to include the *.cpp files in the second project too. You could use a shared folder for that, rather than pick the files out of the other project. – Peter R Feb 20 '14 at 20:21
  • Are header files not enough? Adding all the cpp files seems kind of weird. But thanks I will try it for sure. – Vecihi Feb 20 '14 at 20:25
  • No, the headers are not enough, unless you move your implementation into the header - you may have heard of "header only libraries". – Peter R Feb 20 '14 at 20:27
  • Yeah, actually I'm using "not use precompiled headers" option in the project confiuration. Do I have to add cpp files to Linker>Additional Libraries? (sorry I'm not in front of the computer now) – Vecihi Feb 20 '14 at 20:32
  • No, don't add them to additional libraries, add them as you would any other cpp file in your project. – Peter R Feb 20 '14 at 20:48
0

You have few possibilities to do this. It depends on what these two projects are.

1) They are both applications (compiled to *.exe):

  • Attach source file. In console app, you have filter called 'Source files', where the *.c and *.cpp files are attached. Click on this filter with RMB and select 'Add -> Existing item' and select .c/.cpp file, which contains definition of a class (probably named a.cpp) -> this is exactly the same file that is attached to QT project.

2) QT project is a library (either static [.lib] or dynamic [.dll]).

  • Use solution listed above. In case of dynamic library, if you export class a or some of it's members (and these members are marked with some variation of declspec macro), make sure that this macro will be resolved to empty expression.

  • Add QT project (let's call it QTP) as a dependency. Include header for a (#include "a.h") and use a the very same way you are doing it now. Also, open QTP properties, and copy content of 'General -> Output directory'. Then, go to console app's properties and select 'Linker -> General', then paste copied path to 'Additional library directories'. In 'Linker -> Input' add QTP output name to 'Additional dependencies'.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
0

As @PeterR said you could include the *.cpp files in the second project too. The second project, in fact, need the behaviour of the class, not only its header, and you can give it to the compiler either in the form of source code or in the form of a compiled library.

But I personally would compile the code in a static library.

I know it could seem overkill at a first glance, but it takes only 10 minutes in a reasonable IDE and you can version it, change it in project A without affecting the project B but also use the newer version in the project B when eventually you will feel like. You neither need to bother someone else changes things without warning you and so on.

Can be ten wasted minutes but can as well make the difference in the future.

Nowadays I wish I did like this a couple of times years ago in small projects that were not foreseen to become what they became!

After compiling it as a lib, you'll have as usual with libs to include the .h file and link the .a file

dirluca
  • 423
  • 3
  • 12