Visual Studio: Include .h or .cpp in two-project solution - c++

I have a Visual Studio solution containing 2 project: main project and test (via googletest). In main project I have myclass.cpp and myclass.h files. When I'm trying to compile test project, there are bunch of LNK2019 errors when I include "myclass.h" in my test.cpp file, but everything works fine if I include "myclass.cpp" instead. Is that normal? As far as I know, including of .cpp files is not recommended and generally can be avoided. Any suggestions?

It's normal. If you have 2 projects, 2 binaries will be generated.
Don't include the cpp file.
Instead, link the binaries together.
main project - generates .lib file and either .dll or .exe.
test project - includes header from main. You need to add the .lib generated by main in the additional dependencies of the test project. Somewhere in the Project Settings - Linker Options - Additional Dependencies.
You can generate both .exe and .lib file from a single project. To do this you set:
exe in Linker -> General -> Output File
lib in Linker -> Advanced -> Import Library
You may also need to mark exported functions with __declspec( dllexport ) in the .exe project (see docs), otherwise compiler won't generate a .lib file.

Steps to Use Classes form another project (Add header and solver linker errors)
1) To be able to add the header from another project, first go to "Properties > c++ > General > Additional Include Directories" and add the directory that contains the header. Now you will be able to add the header of the class from the other project, but running the project will still cause Linker Errors.
2) Add __declspec(dllexport) before the class you are using for the other project. This can be added in the header file of that class. This should be added right before the function or variable or class name. Now you will get a lib file. (if placed in wrong place, you can get this warning: https://msdn.microsoft.com/en-us/library/eehkcz60.aspx)
3) "Properties > Linker > Additional Library Directories". Specify the location of the lib file that is generated.
4) "Properties > Linker > Input > Additional Dependencies”: Add the name of the lib file.

Related

MSVC use additional include directories of referenced static library project

I have two projects in my C++ MSVC solution:
A static library project
A .exe project that has a reference to the Project #1 (the static library project)
Project #1 has additional include directories, C:/addincdir .
Project #1's header file, p1header.h includes the header hd.h from C:/addincdir.
Project #2 includes p1header.h .
But when I build the solution, I get an error: Project #2 cannot open include file hd.h (cannot find/locate the header file.) I know the solution is to add C:/addincdir to Project #2's additional include directories.
But is there a way for MSVC to automatically add referenced project's additional include directories? Or is there some kind of macro like $(Project1additionalincludedirectories) that includes Project #1's additional include directories and I can add this variable to Project #2's additional include directories?
It just wouldn't be practical to copy-paste every additional include directory from Project #1 to Project #2.
No. There is no way to automatically add the folders.
(step 5 in Walkthrough: Create and use a static library - Use the functionality from the static library in the app)
To include a header file (listed in the additional include folders) you need to use <... >
#include <header.h++>
You can use an environmental variable such as using MORE_INC_DIR and set it to C:/addincdir1;C:/addincdir2;C:/addincdir3; and put $(MORE_INC_DIR) in the additional include directories of both projects.

Visual Studio Additional Include Paths?

I'm working on an existing Visual Studio 12 "Solution" from another team..
In one of its "project"s,, there's a header file that includes a file from another static library projecet..
So Lets say the solution structure looks something like this:
Project A (Static Library Project)
\__ someFile.h
Project B (Static Library Project)
\__ someLibrary.h
In someFile.h,
...
#include "someLibrary.h"
...
and this loads the library successfully.. BUT this is strange because the path to someLibrary.h is NOT SPECIFIED anywhere in Project A's settings!!
I've checked Configuration Properties => VC++ Directories => Include Directories but path to someLibrary.h is NOT specified here..
Since Project A is a static library with NO cpp files (has only header files), It does not have a Configuration Properties => C++ => Additional Include Directory Option at all... (It only has the Librarian option)
And of course someLibrary.h is not in the same directory as someFile.h.
Finally, in Project A's Common Properties, Project B is not referenced either...
So my question is : How the hell does someFile.h know where someLibrary.h is?
How is it including it??
Is there any other places in project settings/etc where additional include directory can be specified..?
UPDATE:
when I right click on the #include statement and click "Open Document 'someLibrary.h'",
I do see a following VS error box:
File 'someLibrary.h' not found in current source file's directory or in build system paths.
Nevertheless,, there is NO build error, and Project A uses someLibrary.h with no problem!!

Visual Studio Include cpp file from other project

I have a C++ .h and .cpp file from another project that I want to include into my project.
I don't want to copy the files over into my project since I want any changes to those files be applied to both projects.
I've included the directory of the file's folder in the
Properties->VC++ Directories->Include Directories
I've also included the folder in the
Properties->C/C++ -> General -> Additional Include Directories
The .h files seem to work. If I rename the include to anything other than
#include "myfile.h"
The cpp file gets unknown definitions.
When I compile. The error is
fatal error C1083: Cannot open source file: '..\..\..\..\..\..\my project\myfile.cpp': No such file or directory
If I remove the cpp file from the project. Then I get a long list of unresolved functions.
error LNK2019: unresolved external symbol "public: unsigned long __thiscall myclass::myfunction"
How can I include both the .h and .cpp file into my second project?
For cpp files you can just use right mouse click on project, select "add"->existing item.
Then they should compile with others, when a build initiated.
Slightly more complicated with headers. There is two ways to use #include directive:
with < > and " " braces
When " " braces used, compiler treats path as relative (if not absolute used) to location of cpp file.
When < > braces used, compiler looks for file in something like system include folders. For example - stdlib headers folder and windows.h location folder. Properties entry Additional Include Directories also goes there.
I suggest you to change projects structure and extract shared features from both projects to compile it as static library. Place shared headers in some subfolder inside library project and refer then as
#include "mylibHeaderDir/someheader.h"
In dependent projects, after setting Additional Include Directories you can refer theese includes as
#include <myLibHeaderDir/someheader.h>
This approach will help you in future, as you can reuse that shared module in every project you want.
About how to create and link static library you can read this article http://msdn.microsoft.com/en-us/library/vstudio/ms235627(v=vs.110).aspx Version of visual studio may be differ, but basics are the same.
You can't just pick files like that. There are two reasonable ways to solve it. 1, shared the file by means of a Code Versioning System (e.g. svn/git). 2, compile the the .cpp into a library and link to that library.
If the cpp can be used by multiple projects, it must mean that the code is something common. That means you should compile that code by itself into a library and then share that library. Compiling the same cpp into multiple libraries is likely to result in conflicts later if two such libraries are ever needed to work together.
Try to drag them into your solution?
You can create a new folder in your solution, and drag them all into this folder!

How to include .obj files into the project

I have the following question:
I was given the task - to build an application. There was a ready file counter.h and some other file - counter.obj. It turned out that in the counter.h there were only declarations of the functions - how can I include .obj file into the .cpp file so that it compiles? I am using Microsoft Visual Studio 2010 - and in which folder should the file itself go?
Add the obj-file to the Solution just as you would do with cpp-files (i usually do this by drag-and-drop, that is, drag the file from the Windows Explorer and drop it on a project in the Solution Exporer window).
You can put the obj-file together with cpp-files; it doesn't really matter.
You do cannot include object file in to a cpp file.
The compiler compiles the cpp file and generates the obj files, for each cpp file, these files are further linked together to create an libray or an executable.
Usually, you would link libraries(.lib or .dll) to an Application, Check if those are with you.
If not,
You can try linking the object file to your application by:
Go to project properties then from "Property Page" select the node "C/C++" their you will get "Additional Include Directories" add the name of your object file.Keep your obj file in the directory where your source code is or you can add the directory from:
Tools->Options->Projects and Solutions->VC++Directories.
I have never tried the second method except for academic projects,which was years ago, So not sure about it, Please check information on MSDN.

How to include header files in Visual Studio 2008?

I am currently trying to compile a simple program that includes two header files. I see them in the Solution Explorer, where I included them through "include existing files". However, when I run my program it get the following error.
fatal error C1083: Cannot open include file: 'FileWrite.h': No such file or directory. THe problem is that I see the file included in the Header's folder and in the code I have written:
#include "FileWrite.h"
and then the rest of the program code.
Is there something else needed to do so that the compiler can see the header file and link it to the .cpp file I'm trying to compile?
If you write in your code something like #include "FileWrite.h" you need to make sure compiler can find that file. There are three options:
FileWrite.h should either be in the same directory as your source code file (.cpp) or
Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories) or
Path could be set in your VisualStudio - add it to Include Files in Tools->Options->Projects and Solutions->VC++ Directories
Which of these options shell be used depends on whether that header originates from this project (1st option) or some other project (any of other two options).
There are two ways to do this.
1) Only for the current project
Select your project -> properties -> C/C++ -> General -> Additional Include Directories -
Include your header file directory.
2) For all projects
Tools -> Options -> VC++ Directories -> Include files - Add the header file directory.
Refrain from using 2, as it would be difficult to figure out dependencies for a project when compiling it on a system different than yours.
When including files the compiler first looks in the current directory (the directory which contains the source .cpp file) then it looks in the additional include directories. If FileWrite.h isn't in the same directory as your source file check the additional included directories.
In the project's property page look at the additional include directories and see if they include the folder in which FileWrite.h is in.
You said the file is in the "headers" folder. This could either mean the headers filter or an actual headers directory on the filesystem. When including a file from your own project you need to specify the path from the file you're including into. So, if you had something like so:
src/main.cpp
include/my_object.h
You would use #include "../include/my_object.h" in main.cpp.
That's for directories. The folders you see in your project are called filters and have absolutely no relation to the directory structure of your project unless you force it to. You need to be paying attention to what the structure looks like in windows explorer to ascertain what path to use in an include statement.