Bojack
Bojack

Reputation: 479

Object files with visual studio

I have a main file main.cpp which include myclass.h. I also have myclass.o and myclass.cpp. The visual studio project is only using the files main.cpp and myclass.h.

This compile:

#include "myclass.h"
int main()
{

   return 0;
}

This does not:

#include "myclass.h"
int main()
{
   myclass M{};

   return 0;
}

which gives a link error - probably because the myclass.cpp or myclass.o is not included in the project.

I would prefer to include only myclass.o since compiling this class is very problematic. See https://forum.qt.io/topic/8492/solved-cannot-get-quazip-test-to-work/3 if you are curious of which library I am using.

To my question: Can I compile and use the class with only the header and the object file, i.e without the .cpp file at all?

Thanks!

Upvotes: 1

Views: 10081

Answers (1)

Jon Trauntvein
Jon Trauntvein

Reputation: 4554

Visual Studio generates a .obj file as its intermediate format. gcc and gcc are the ones that produce a .o file. You can include this in your project or, since libraries rarely have a single compile unit, you can build a .lib file and include that. Be warned, though, that the compiled units must be compiled using the exact same compiler switches such as run time library and etc. as you are using in you project. It is often simpler to simply compile the file from source.

We can also talk about using a DLL but that seems to be beyond the scope of your question and also introduces the complication of calling conventions.

Upvotes: 1

Related Questions