How do I tell Visual Studio to include a library's .cpp file? - c++

I am using lodepng to produce compressed image files. I have stored the .cpp and .h files (there is only 1 of each) in C:/Program Files/Common Files/lodepng/lodepng, and I want my visual studio project to automatically look in these folders.
I have added C:/Program Files/Common Files/lodepng to my Include Directories, so wherever I have #include <lodepng/lodepng.h> it not picks up that header file, which is nice.
However, when I try to compile I get a linker error (obviously) because I haven't told VS where to look for the .cpp file (it's in C:/Program Files/Common Files/lodepng/lodepng/lodepng.cpp). But how do I tell it that?
Currently the fix I'm using is to simply drag and drop loadpng.cpp into my Source Files of the project, but this isn't a very nice solution.
Any ideas?

You have to add your .cpp files to (compile with) your project.
Right click the project in the solution explorer, then select Add -> existing item and select your .cpp files. Note the folder the files live must be inlcuded to your diretories

VC++ would only look files for you when encountering directives like:
#include "lodepng.h"
Though possible it is not a common practice to include a cpp file, in fact you could include text file for example to include very large data file etc.
Best practice is to add cpp file into your project, don't put any cpp file in a header folder and don't include them unless there are no any alternatives.

Related

Esay way to include all header files from solutions explorer Visual Studio 2019

I want include all header files from solutions explorer like this:
without add all directories with this option:
Is there an easy way to tell VS2019 to use and link all header files from solutions explorer automatically?
Why?
If I have a lot of source code directories and in each directory are the header files... I need to add each directory manually.
Other simple Example:
The directory structure it this one:
And I need to add #include "test2.h" in "test1.c" and in this case VS cant find the header file. So the header "test2.h" is NOT where the test1.c is. Why VS dont find the header automatically by solutions explorer?
Ok. There is no solution.
Possible alternatives:
All headers are in one place and one include path is required.
Or headers in the same directory like the source code files.
Or headers need to be include like #include "../../header.h"
Thanks.
VS cannot provide the way of automatically including header files in all .cpp files. If the header file and the solution are in the same path, you don't need to add path in Additional Include Directories. If the header file is not in the same path with project, you only need to add the path.

How to include .cpp and .hpp files of a library in my c++ project in visual studio

Apologies for a very basic question. I am trying to port from a CMake project into a visual studio project.
Basically I want to run the project from visual studio without using the CMake file. In the project I need to port, there are many folders and sub folders that contains many .cpp and .h files. These are included to the main cpp file as using #includes.
My Case
The library I want to include
and my main.cpp
For instance to make things simple assume I have a main file main.cpp and this file includes #include "Libpfs/colorspace/colorspace.h". The Libpfs is a folder and it has many sub folders one of which is colorspace folder and this has many .h and .cpp files. One .h file is colorspace.h that is included in main.cpp using the #include and the folder also has .cpp file i.e. colorspace.cpp.
My Attempt
My objective is include them to my project.
Now here is what I have tried
in Visual Studio Project->properties->C/C++ in Additional Include Directories I gave the path of the folder that contains Libpfs but this approach did not work and gave linker errors this might be because I have no lib files for the Libpfs (correct me if I am wrong). I only have .cpp files of the corresponding .h files. I presume the cause of error is the the .cpp files are not compiled yet.
My Question
How can I include the cpp file to my project as well (not the lib files since I dont have those).
Using this for a source, I find the CMakeLists.txt to contain nothing special.
FILE(GLOB COLORSPACE_H *.h)
FILE(GLOB COLORSPACE_HXX *.hxx)
FILE(GLOB COLORSPACE_CPP *.cpp)
SET(LIBPFS_H ${LIBPFS_H} ${COLORSPACE_H} ${COLORSPACE_HXX} PARENT_SCOPE)
SET(LIBPFS_CPP ${LIBPFS_CPP} ${COLORSPACE_CPP} PARENT_SCOPE)
So you can just add all the files to a VS C++ project. I would use some directory management, to separate these sources from your own.
But anyhow, in that case, you should include the sources by relative path. E.g.
#include "../../Libpfs/colorspace/colorspace.h"
Alternatively, you could put everything in a separate C++ library (static .lib or dynamic .dll). In that case you should but the binaries in in a bin path and add that as additional library directory (project properties of your own project) and put all the header files in an include/Libpfs path and add that as additional include directory. In that case you should include the files as.
#include <Libpfs/colorspace/colorspace.h>
On another topic
#define pow_F(a,b) (xexpf(b*xlogf(a)))
I found this define only in the sources of the same source used above.
It seems to be sourced from sleef and according to this it should give a speedup. But you should measure if that is really still true, instead of doing copy-paste/cargo cult programming.
I think generally you should use the standard library std::pow, which has overloads for float, double and long double. The compiler will in most cases optimize its use for you.
open explorer , look for C:\Program Files\Microsoft SDK\ and then go after dir with alot of .lib's in its \lib .
it's an msvc source for .lib and other stuff like that . copy your library there

Visual Studio don't see header files

Welcome
I have 5files in my project
main.cpp
ClassOne.cpp
ClasOne.hpp
ClassTwo.cpp
ClassTwo.hpp
In .hpp I put defintion of my Class,and in .cpp declarations(of methods,constructors etc.)
I use Header guards in .hpp file,in .cpp include only ClassOne/Two.hpp
Then when I try to compile my code I get errors in .cpp files because these files dont see their .hpp(declarations).
In main file including ClassOne.hpp and ClassTwo.hpp.
What's the reason?
PS I disabled Precompiled Headers
Be sure to add both the hpp files and the cpp files to the solution. It's something like Solution Explorer > Project > Add Existing and make sure the proper files are included in the project, not just present in the directory.

Visual Studio C++ able to compile with compile errors (red underlines)

I am having a problem of getting compile errors (red underlines) like:
Error: cannot open source file "stdafx.h"
Here an edited screenshot of the environment:
On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off.
I am working on a school project, and each Folder are the source files of different parts of the project with different people who are in-charge of them.
For example, Student A and B are incharge of AST and PARSER folders (we will call them sub-projects).
We have an API for each sub-project so other sub-projects know what to call.
At the TOP-CENTER, we have my Source File for a class QueryProcessor. (just the first few lines)
Below it, is the Output for the Build Success.
The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment.
On the RIGHT, that is the stdafx.h where we include all the different sub-projects so we save the trouble of each project having a different stdafx.h
However, I am able to build the project. I am pretty sure I am doing this directory/linking wrongly.
This should work
Right click on the solution file
Click Open in Windows Explorer
Find file stdfx.h in explorer and copy the path of the folder
In visual studio solution explorer, Right click on the project file
Click properties-> C/C++ -> General
In the Additional Include Directories paste the path
Combining folders and virtual folders in VC is from my point of view messy because the virtual folders indicate that all files are in one directory and the folders created on the harddrive obviously indicate that all files are in different directories. You can combine it if you know what's going on but in your case I would not recommend it.
I assume you missunderstand the purpose of stdafx.h The purpose of this header file is NOT to put all header filles into it and then just include it to all other files. Here is a SO question about this Purpose of stdafx.h
After cleaning up your stdafx.h file include as many header files into your .cpp files and only put these includes in your header files if they are required in the header file
Turn on show all files, now you will work with actual folders and you can be sure that if you adress a folder like "PKB" that this folder really exists since you can see it in the left solution explorer.
If you use using namespace std; for example make sure you also include the required header files. You might think "hey I already included e.g. iostream in another header file which I now include in this header file so I don't need it" That will really destroy you when you work with bigger projects.
Oh and regarding the stdafx.h include problem as soon as you switch to show all files I assume you will realise that stdafx is in a different file than the file where you use the include. Maybe something like #include "..\stdafx.h" is required (depending on your structure).
I think it's obivious but if you include a header file the include is allway relative to the file which is including the other header file.
stdafx.h is commonly used for creating a precompiled-header, which essentially is a compile-time optimisation such that the compiler will not continually compile these headers for every compilation unit.
If any of these headers changes, you will need to do a full system rebuild.
In reality it is preferable only to use it to include standard headers plus third-party headers (like boost libraries and similar) that you are not ever going to change.
You may decide that some of your own libraries are "set in stone" and can also be included.
Every project, i.e. every part of the project that is built into a separate unit (DLL or .exe) should have its own precompiled header and its own version of stdafx.h
Projects should only ever include their own .stdafx and not those of other projects, therefore this header file can also be used to define your dllexport macro.
When arranging your project headers you should be aware of:
1. Which headers are included externally
2. Which headers are only included internally, and are not even included indirectly externally.
The latter sort should include your stdafx.h file and should ideally not be in the same directory as those headers included from outside your project.

Include file into entire project

is there any way to include one file into entire project?
I want to have some globals and macros accessible from any file in project, but i do not really write #include in every file
TY
Use Visual Studio project settings:
Projekt Properties->Configuration Properties->C/C++->Advanced->Forced Include Files
This will use the specified include files in every source file.
MSDN Forced include
Alternativ:
If you use precompiled headers you could also add your globals/macros to the stdafx.h