0

I have several functions in a .cpp file and I want several of my projects use the same file. So instead of copy them to different directories just add them (from original location) to project file in visual studio and use them.

The problem is #include "stdafx.h" seems to refer to the same directory as the file exist and this causes compiler problems.

Is there a way or workaround or am I doing something conceptually wrong?

(I think making it as a static library is a bit overkill. compiling and building new project and making sure others are using correct DEBUG/RELEASE .lib. Hmmmm. A bit hard for a lazy coder!)

Roozbeh G
  • 427
  • 4
  • 18
  • Create a static library project with just that file. Have all other projects depend on the library, rather than directly mention the source file. – Igor Tandetnik Jul 18 '14 at 00:59
  • If you don't need to use precompiled headers for this file, then get rid of `#include "stdafx.h"`. Then right click on the file in the Solution Explorer, select properties, and somewhere in the options pages for C/C++ there's an option to disable precompiled headers. You could also switch them off for the entire project. – Praetorian Jul 18 '14 at 01:43

1 Answers1

1

I think what you should do is to include that ".cpp" file in a library (.DLL). Then you can statically load it in your project files by including that library (.LIB) and the header file of the library that exports the functions (.H) into the projects that are using the library. That way you will statically load your (.DLL) into memory using the (.LIB) file and the functions will be available through the (.H) file that you will create when making your library.

I know saying this was is not that obvious, especially if you haven't done it in the past. If you want, you can upload your project files onto some location and give the link. And I can give you a better description of what you need to do.

santahopar
  • 2,933
  • 2
  • 29
  • 50
  • I think it is a bit overkill. I have to create a new project, with every changes in cpp file also compile it and make sure other projects are using recent .lib file (or .dll). Your solution would work but hoping for even easier solution :D – Roozbeh G Jul 18 '14 at 01:03
  • Well, it is not a good practice in general to link to the .cpp file from different projects by including the header file. Why do you have several projects? Any particular reason? Can't you just do everything in one project? – santahopar Jul 18 '14 at 01:06
  • stdafx.h is a good placeholder if you are using the same header file in different source files. So instead of including your header file in every single of your source files, you just include the header file in your "stdafx.h" and include "stdafx.h" in your source files. So if your source files are using std::string, you won't have to write #include in every single .cpp file. Instead you #include in stdafx.h and and #include "stdafx.h" in your source files. – santahopar Jul 18 '14 at 01:09
  • Well because they are totally different projects with different purposes just sharing some general algorithms. Imagine I have a general sorting algorithm which I want to use it in multiple projects. So you mean the best practice is to make it as an .lib file? – Roozbeh G Jul 18 '14 at 01:27
  • I think so. I don't see why would you do it in any other way. Especially when they are so generic as sorting algorithms. – santahopar Jul 18 '14 at 01:35