Header&Implementation files Visual Studio 2017 C++ - c++

Why in Visual Studio 2017 the compiler will complain (LinkError 2019, seems to not compile the implementation) if I include my headers and implementation files rather than use the default pch.h and pch.cpp files? Obviously all the files mentioned are in the same folder.
(Note: Copy-Paste the same code in pch.h and pch.cpp will work fine)
Im using this sintax on header.h:
#pragma once
//init code
Implementation.cpp:
#include "header.h"
//impl code
Main.cpp:
#include "header.h"
//main code

Related

C++ project is not finding its header files (visual studio)

I have not used C++ in visual studio in a lonnng lonnng time so probably this is a basic question but I would like some help.
I am opening someone else C++ code and the first thing I notice is that it is not recognizing its includes.
For example I have a main.cpp that has
#include "stdafx.h"
#include "myheader.h"
this cpp file is inside a "source" folder and those header files are under a "headers" folder.
I got an error "cannot open source file "stdafx.h" and "cannot open source file "myheader.h"
It is been ages I haven't touched C++ in visual studio. What configuration I should do to fix this?
if source and headers folders are in same level in folder structure you can use
#include "..\headers\myheader.h"
Similarly locate your stdafx.h and add the relative path to the .cpp file.

Cannot find header file in Visual Studio project directory

I am trying to build a simple Windows 32 Console application pong game using Visual C++. See link http://www.noobtuts.com/cpp/2d-pong-game for more information. I have downloaded a group of headers files as well as a DLL file and libraries in a folder called freeglut_files.
Structure:
/GL
/freeglut.h
/freeglut.lib
/freeglut_ext.h
/freeglut_std.h
/glut.h
/freeglut.dll
/freeglut.lib
In Visual Studio Community 2015, my project structure looks like this:
/Pong
/External Dependencies
/Contains a number of files including GL.h and GLU.h (see below)
/Header Files
/stdafx.h
/targetver.h
/Resource Files
/Source Files
/freeglut.dll
/freeglut.h
/freeglut.lib
/freeglut_ext.h
/freeglut_std.h
/glut.h
/Pong.cpp (executable)
/stdafx.cpp
/ReadMe.txt
I added all the files, including the GL directory, to the source files directory of my Visual Studio project.
For some reason Visual Studio puts some of the header files in the /External Dependencies folder, including GL.h and GLU.h. However, my compiler cannot seem to find freeglut.h. I have tried moving it to the /Header Files directory and cannot move it to the /External Dependencies directory. Here is my code to Pong.cpp
#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "freeglut.h"
#pragma comment (lib, "OpenGL32.lib")
int _tmain(int argc, char** argv)
{
return 0;
}
The compiler will not compile the line where I include freeglut.h. Where should this file be? Why can I not add it to /External Dependencies?
Have a look at page Team Development with Visual Studio .NET and Visual SourceSafe. It might help problems in the future as well. It's always good to try to find the answer from the developer of the software you're using.

Why does Visual Studio error when linking having files included?

I don't really understand the why behind this, but I think I've figured out the what.
My brother is working on a homework project and they're using Visual Studio. He was having issues getting his project to compile. The project in question consisted of:
main.cpp
classes.h
class_functions.cpp
general_functions.cpp
They had the following import structure:
main.cpp
#include "classes.h"
#include "class_functions.cpp"
#include "general_functions.cpp"
class_functions.cpp
#include "classes.h"
general_functions.cpp
#include "classes.h"
He kept getting link errors when trying to compile, even though g++ would compile it just fine. So I tried via Visual Studio command prompt and cl. This compiled just fine, so I tried msbuild which (obviously?) failed. I went into the vxproj file, because hey, what is it telling cl that I'm not?
In the proj file it was including classes.h, general_functions.cpp and class_functions.cpp, as well as main.cpp. When I removed general and class functions, msbuild ran just fine. Put either of them back and boom. classes.h contains
#ifndef myclasses
#define myclasses
/* Code here */
#endif
And the other .cpp files contain similar directives.
I finally figured out how to eliminate the problem - eliminate
#include "general_functions.cpp"
#include "class_functions.cpp"
from the code base, and now Visual Studio compiles this just fine.
The question is why? What is the justification or possible benefits from doing this?
the difference between .cpp and .h files is that the .h files are usually included with the #include directive while .cpp files are included in the linking process.
The #include instruction will insert a copy of the included file in the code you are compiling, so by including the .cpp files you will compile a single source file that contains all your .cpp files. It's very bad practice but still could work.
But if you are then adding the .cpp files also in the linking step then you are adding those files a second time creating probably duplicate definitions of your functions, e.g. linker error. I think that's probably the main error (without the linker error codes I'm just guessing).

Visual C++ Precompiled Headers errors

Update:
What are the effects of including stdafx.h in my header files?
I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows.
In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h.
Here's my stdafx.h
#pragma once
#include <string>
#include <vector>
#include <map>
...
and my stdafx.cpp
#include "stdafx.h"
In every .h and .cpp file, I have the following:
#pragma once //if in a header file
#include "stdafx.h"
For both release and debug, I have "Create Precompiled Header (/Yc)". It compiled fine in debug mode, but in release mode it keeps reporting
error LNK2005: ___##_PchSym_#00#UfhvihUaszlaDUwlxfnvmghUnnlUhixUnnlPeDUnnlPeDUivovzhvUvmgrgbOlyq# already defined in A.obj
If I switch both to "Use precompiled header", I get in both Debug and Release
fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object file:
Does anyone know what's going on?
You put "create precompiled header" only for stdafx.cpp. Then "use precompiled header" for all of the other ".cpp" files. Finally, have include "stdafx.h" at the start of each ".cpp" file (not usually in the header files.
The /Yc compiler option is used to create a pre-compiled header for a compilation action. The /Yu option instructs the compiler to use a pre-compiled header.
You will always use the /Yu option in project settings.
In the property pages for your stdafx.cpp file, the /Yc option will be set.
It is important to understand that there are separate compilation options for each .cpp file
.
See here for details of the /Y options.
You put the #pragma once before the #include "stdafx.h" which I think is causing the compiler to ignore the #pragma once directive.
Also, I don't think you should be putting the #include "stdafx.h" line into the header files at all.
The results of using "stdafx.h" are not influenced by the PreCompiled Header system. If you turn off Create PCH/Use PCH, the code compiles and creates the same output, except it does so slower. This is also why you can use it in portable code (unlike #pragma once)

Header file without source file

I have written the body of the function in the header file and so do not have a source file. when I tried running my project in visual studio .. I got an
error: Cannot open source file: No such file or directory.
How do I make visual studio understand that the definitions of the function are within the header itself?
You need to create a dummy source.cpp file just containing #include "source.h"
edit - I just tried this - Visual studio will let you do.
test.cpp
#include "test.h"
where test.h
#include "stdio.h"
int main()
{
printf("hello world");
return 0;
}
Interesting - but pointless !