two projects using same .cpp file & header which has include "stdafx.h" - c++

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!)

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.

Related

How do I share C++ source code files between projects in Visual Studio?

I'm writing a cross-platform application. One version will work under Win32 and the second on Windows Phone.
I'd like to reuse my C++ core - especially that there are no platform dependencies inside, STL only. I order to do so, I want to use the same source files in two projects: static Win32 library (.lib) and Windows Phone Component (C++/CLI).
How can I configure these two projects to use exactly the same source and header files?
OK, let's take an example. Let's say, that I have project:
MyApp.Library [win32 lib]
myClass.cpp
myClass.h
This library is compiled to .DLL file and then imported in a Win32 application:
MyApp.Win32App [win32 C#]
Since Win32 is not compatible with Windows Phone on the binary level, I cannot use that library directly. But since the library uses only STL, I can create a Windows Phone component, put all its sources there, and build.
MyApp.Component [Windows Phone component]
myClass.cpp
myClass.h
I want these two files to be exactly the same ones as used in the library. How should I organize the project to achieve this effect?
You can add source code from a common location to multiple projects. I do that a lot; I have code in a common directory that is at the same level in the directory hierarchy as most of my project files. It's simply a matter of adding .h and .cpp files from the common directory to your various projects.
I did notice that VisualStudio gets a little cranky and difficult if you use a network drive for common source, so I don't do that. But so long as all of your source code is on local disks, and the IDE knows where to find them, there is no problem having the same source file in very many projects.
one way is to put all .hpp and .cpp files into a seperate folder - call it "Shared".
Then add an additional include Directory inside your solution configuration property - this directory must be a relative path.
Then add ONLY the .cpp files relative from that shared folder into your project
NB! If you have include "stdafx.h" inside your .cpp files in the Shared folder, then comment those out.

Linking a static library causes errors in the linked library

I have a small library project that uses OpenGL (glfw and glew). Now, the project compiles fine, but when I create a new project and statically link the library project, VS starts to throw errors in the library project. Why is that?
More specifically, I get this error:
error C1083: Cannot open include file 'GL/glew.h': No such file or directory (file: trenums3d.h)
The project setup is like this: There's the library project 'Foo', which is compiled into a static library ('Foo.lib'). The application project 'Bar' links 'Foo' (I added the folder where Foo.lib resides to Bar's 'Additional Library Directories', as well as the source folder of 'Foo' to Bar's 'Additional Include Directories'). If I compile only the library project, everything works just fine, but compiling the whole solution give me the aforementioned error.
This isn't a proper answer to your question, but just an explanation of the steps required for building an application in a compiled language.
Building a project containing multiple files is a three-step process:
Creation and editing of source and header files
Compilation of the source files (this step contains many sub-steps). This step creates object files of all translation units
Linking of all object files and libraries to form the final executable
Error like the one shown in your question is emitted in the second step. Linking with libraries happens in a completely different step, and is usually done by a different program than the compiler.
To answer your question, if linking with a static library also requires linking with the other libraries that the static library depend on, then the answer is normally yes. Static libraries only contain the function in the actual libraries, you can look at a static library more as a collection or archive of object files. Static libraries does not contain any information about other libraries they depend on.
And as for your problem, with the pre-processor error, it's because you include a header file from your static library, and that header file in turn includes some header files. But the pre-processor doesn't have the secondary included header files in its default search path, so you need to add it.
This still have nothing to do with linking any library, this is a pure pre-processor issue, and is handled in step two in my list above.
I suspect the header files of your static library look like somewhat this:
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "GL/glew.h"
// ...
#endif
If you include this header file from another library or application, the compiler will open this file and will see that it needs to open GL/glew.h as well in order to be able to "understand" the definition of your class.
This means you need to supply at least the header files of glew. The only way to get rid of this is if you manage to only reference glew files from your .cpp files but not from your .h files. In some cases, forward declarations can be used, but not sure if this will work for glew.
Concerning the linker settings: In case your glew library is built statically as well, you may or may not have to supply that library file and link to it from your project. This depends on how you setup your linker for your own static library. If you have troubles in this step, create a new question.

How do I include my C++ DLL in a separate project?

This question might seem obvious but I am having a lot of trouble with this, and I have ended up having to post here after a lot of searching.
I currently have two windows of Visual Studio open. One is a Win32 Console->DLL project which exports a class, and in the output directory I have:
.dll file
.exp file
.pdb file
.lib file
I have dropped the DLL file into the my other project's output directory, as I do with all DLLs, and that works fine usually. Then, I added the directory into the Linker's library directories.
But unlike most libraries I use, I think I have done something wrong or I misunderstand how this works, I have no .h[pp] files, and so I have no idea how I am supposed to include the functions into my code. I'd rather not have Windows-only hacks (I want to confine that to the DLL project only, so that it can be ported easily).
Can anyone enlighten me as to what I am doing wrong?
There is nothing 'hacky' or 'windows' specific about having .h files available to the other projects. Your .lib file will provide the necessary information to complete the build. See: How do I use a third party dll in Visual Studio C++?
Did you add the .lib file corresponding to the .dll into the other project's directory?
It is the .lib file that is consumed by the linker, not the DLL (which is consumed by the loader at run-time).
A .dll is a shared library, as opposed to a static library (.lib on Windows).
Static library must always be linked when you compile your project, and you can easily call their functions using header (.h/.hpp) files, whereas you have two options for the shared library:
static linking (at compile-time, but the way to do it is different than for a static library)
dynamic linking (at run-time)
I would advise you to read this in-depth article: http://www.codeproject.com/Articles/85391/Microsoft-Visual-C-Static-and-Dynamic-Libraries
See also the wikipedia article: https://en.wikipedia.org/wiki/Dynamic-link_library

Working with DLL in a C++ application

I'm planning to use id3lib in my application. I'm looking for a way to use the library as a DLL. I've noticed that they released the library in various form: one of which is windows binary. My target platform in Windows, and I'll use Qt 4.8. After I extract the files in windows binary, I found the following files in Release folder:
id3lib.dll
id3lib.exp
id3lib.lib
I know how to use a DLL in Qt given the DLL, one or more header files where function prototypes resides, and with or without the *.lib file. This package doesn't come with a header file.
How am I supposed to use this package without any header file? What's the purpose of the *.lib and *.exp files here? As far as I know *.lib files are used for static linking with functions which I don't want in my program.
You've just missed the header. It is available under the include subfolder (see here), also the .lib file is still needed for linking, even if you'll be using the DLL.
The usual course is to use a header file #included in the C++ file, the .lib file to link to and the .dll is required at run time.
The header file should/may be in another package as the same header is probably used for different kinds of linking strategies.
At worst you should be able to use a tool such as depends.exe to view the exported symbols and create your own h file to match - but it would be better to find a .h file issued with the release.

How to include a C/C++ lib (e.g. libcurl) in a C++ project (IDE: Eclipse)?

I have a problem, I'm working on a C++ project (I'm using Eclipse as my IDE).
Now I want to make use of a C/C++ library, for example libcurl, so I download curl and don't know what to do next.
Must I only include the .h and .c files I need or must I compile it before?
A typical way to use a library (this, of course might differ from library to library) is to include all of the header files from the library, include them as needed, use functions that they provide. Then, when building the application, link the objects with the library object (.a, .lib, whatever the extension). If you don't have the .a or .lib file, then you should build that independently
You must compile it before, then include the h files in your include path (in the Eclipse project). You will also need to find out how to import the library that you compile into your Eclipse project.
Once all that is done, you can make calls to the library after #include'ing the appropriate headers.
Both ways are possible. Compiling to a library is probably easier.