How to include header files of another projects in the same solution? - c++

I have two C++ projects (proj1 and proj2) in the same solution. How to include the header files from proj1 into proj2? I included those header files in proj2 by setting Additional Include Directories, but cannot link those functions - Error: unresolved externals.

you don't need to from the reference of porj2,add proj1,then the compiler will link proj1 when compile proj2.

So your problem is not the actual inclusion of the header files, it's linking to the object files of the other project.
There are basically two solutions to that problem:
Simply pull in the source file from other project into the project where they are needed. This will cause you to build the source files twice, once for each project. Note that I don't mean you should physically copy the files on disk, just drag and drop the source files in the solution side-bar.
Put common code in a third project, as a library. Then both your projects uses this library, and links with it.
I highly recommend the second solution.

While you are getting linking error that means you have compiled your code successfully and that means that you have included your headers correctly. Congrats! However, you are facing unresolved externals error. This is because you have just tell the compiler where to find the .h file but what about the real code? yes the one in the .cpp?
Options:
Put all your code in .h files which could be a fast solution but not an good idea (unless you need portable solution for templated code).
Build your first project as static library and link with the output .lib file (you could find how to do it by quick googling)
Bring your sources (.h and .cpp) to the other project and build them inside your project (Huge redundancy happened here).

Related

Sharing classes between projects while getting around stdafx.h dependencies

I have a Visual Studio 2010 solution with several very closely related projects. Would like to get away from copy-paste code sharing and share some common classes between the projects. We have tried adding a reference to the class from project A into project B, but are getting compile errors where the shared class is referencing the wrong stdafx.h file. Note that precompiled headers are on (by default) for both projects.
We have also learned that it is not possible to #ifdef the correct stdafx.h file using preprocessor definitions.
At this point it looks like our options are to either build a static library project or multiple projects and reference that from both project A & B, or to try some pre-build event copying of the correct stdafx.h file into the shared-code subfolder. Third would be to re-architect both projects to not use stdafx.h at all.
All of these options seem like overkill to me. Is there an easier solution that I am missing?
The two that popped straight into my head when I was reading your question was to:
turn off precompiled headers.
compile the common code as a shared library.
In your instance 1 would allow you to be more productive quicker, but don't delete stdafx from the project it as that would mean having to go through your project and turning off the precompiled header. Simply remove all the stuff you added and leave it default for both projects. It's been a while since I had to do this, but I think I then would then right-click and compile each cpp file individually. This would give you the missing header files for that compilation unit. Add them and then rinse and repeat for each.
Running code through static analysis would highlight things that could speed up compilation (forward declares vs includes, etc.).
When I have run into this problem I fixed it by:
Rename all stdafx.h files specific to their project (stdafx_prj_A.h, stdafx_prj_B.h, etc.).
Also, you will need to update each solution to use the correct precompiled header file. The setting for this is in Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header File.
Do not forget to make this set for all Configurations and Platforms.
Be safe. Each StdAfx.cpp only needs to include the correct header file. So do not include all of the stdafx headers in 1 StdAfx.cpp file.
Move any classes that are shared between projects to their own project.
This still allows you to use PCH files, and you do not have to worry about the compiler using the wrong file. If I recall correctly VS will use the currently specified PCH file name for any new class that you create.
We create .cxx files with the original source EXCEPT for the #include "stdafx.h". This is the main file that implements the class.
Then we create separate project specific .cpp files that just contain (for example NumEdit.cpp)
#include "stdafx.h"
#include "\pathtoimplementation\NumEdit.cxx"
Then each project's debug and release folders have the project specific .obj file.

Using C++ library that comes as .cpp and .h files

I'm trying to use this library: https://github.com/micknoise/Maximilian to do some DSP and am pretty new to C++ compilation which is leading me to some issues,
I'm using Visual Studio 15 and have successfully included a number of statically linked libraries that come in .lib and .a form but am confused on how to link the aforementioned code into my project since it does not come in a compiled form. Simply putting the unzipped project folder (which contains maximilian.h and then a /lib directory with the rest of the .c and .h files) into my include paths leads to linker errors. Am I missing a step?
Thanks to the help above, I was able to get the .lib file by opening the visual studio project and changing the export from .exe to .lib. I then included that in my project and things worked great.

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.

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

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.

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.