add_executable (or adding source to project) vs including it? - c++

I have been curious about the difference between including a file in another source file with #include filename.h and "adding" a source to a C++ project.
In Visual Studio, adding a source file to a project is done by right clicking and choosing "add existing source to project".
We don't need to do that to a file that is in the "additional directories" path and included using #include.
However, sometimes .cpp files which are not #included need to be added to the project.
So:
When is it that a .cpp file needs to be added to my project?
Also, W/R/T to CMAKE:
When I specify include_directories and point it to the path where I my files to be included are, and those files are referenced in the source, why do I also have to add every header with add_exectuable?
In other words, those files are included with #include and CMAKE knows where to look for them, so what does setting add_executable do?

Related

Is it possible to override an include file from a project dependency

I have a project that uses includes from other projects. This works fine, however one of the includes references a file that I would like to somehow overwrite.
Is this possible? If I include a file with the same name in my project will that somehow overwrite it on compilation? Unfortunately I don't have write access to that file, so I need to find a work around and substitute my own file. This is with Visual Studio 2017.
Put your header file directory to beggining of your include search path "Additional Include Directories". So compiler will find your header file first.

Is it bad that CLion adds header files to cmake source files?

When I create a new pair of .h and .cpp file with clion using the following dialog
if I check "add to targets" and "create an associated header", it modifies the line set(SOURCE_FILES ...) which it later passes to add_executable so that it containes both new_source_file.h and new_source_file.cpp. If I understand C++ and cmake correctly, it is bad, because header files should not be compiled by themselves. Am I right? Is this a problem with CLion?
It's correct to add headers to CMakeLists.txt.
CMake is smart enough and doesn't invoke the compiler on headers, but adding them to the CMakeLists.txt makes sure that they are referenced in the projects generated by cmake (for example, Visual Studio projects and Code Block projects). This in turn makes it possible to show the headers (and not only the .cpp files) in the "project" pane of most IDEs which support cmake.
If you don't add them, the compilation itself should work fine, but the IDE probably won't know that such headers are part of your project, thus they won't be included in the project pane, in the "search in project" function and so on.
From CLion's FAQ:
using set(SOURCE_FILES main.cpp) is how CLion now knows that main.cpp is included in your project.
As for now, header files (in case their names differ from the
appropriate .cpp files already added to the SOURCE_FILES variable)
should also be included in the project in that way.
In your case, you don't need to specify lcm.h when setting SOURCE_FILES, but it also doesn't hurt.

Is it OK to make a stdfx.h file myself?

Now, I realized that I need a precompiled header for my project. However, at the beginning, I created my project as an empty project which doesn't have a stdfx.h header file. So, this is what I did:
- I created a header file name stdfx.h
- In that file, I included some libraries.
- I went to the project Property Pages -> C/C++ -> Precompiled header -> set the PRECOMILED HEADER option to Use (/Yu).
- Finally, I included stdfx.h in every cpp file.
It works. However, I am wondering if the stdfx.h file that I created myself works like the one that is automatically created by Visual Studio? I mean is it really a precompiled header which will save time when compiling or just a normal header file?
It's normally named stdafx.h, but it really doesn't matter. You can name your precompiled header whatever you want.
You are missing one final step.
You also need to create a stdafx.cpp. This is an empty file that only has a #include "stdafx.h" line.
For this particular file, right click on it in the Solution Explorer and select Properties. This will bring up a Properties page with settings specific to this source file (in inherits your project settings by default). For this particular file, select /Yc as the compiler option instead of /Yu for the Precompiled setting. If you don't do this, you may not observe the build speed improvements of precompiled headers.
When Visual Studio builds, it will build your stdafx.cpp file first. And along with that, it will build the .pch file that the other source files will pick up.
And while I'm here. The things to include in the precompiled header file are all the system and C/C++ runtime header files. (e.g. <windows.h>, <stdio.h>, <string>, <map>, etc...). You'll get the most gains by including all these files - since they never change from build to build.
You definitely can make stdfx.h by yourself or other precompiled header file (name is not really important). But you should follow some rules described in MSDN.
You can read more about precompiled headers in the Documentation

Unable to include header file in C++ console application project

I have been trying linking of a .lib file and also including a header file in my C++ console application project. I copied the C++ header file from one of my other projects, and pasted it under Header Files folder in console application project. Here's the screenshot to see: http://i.imgur.com/JFFIn.png
However, when I try to include the header in my code as #include..., I do not get an intellisense with my header file's name. (I only see targetver.h, stdafx.h and Debug folder)
I tried to point Add additional include directories in my C++ console application project properties to the Project folder itself, but that doesn't seem to help and the file still doesn't show up.
If I write the name of the header file as #include "DllTest.h", I get an error saying: Cannot open include file: 'DLLTest.h': No such file or directory c:\users\ht\documents\visual studio 2010\projects\dlltest\dlltestconsole\dlltestconsole.cpp
How is the header file included in here, so that it starts appearing? If I add a new item > Header File, name it to DLLTest.h and copy paste the header files content here, it just shows up normally. How will the header file which is copied - pasted into Header Files folder show up in the code?
In C++ projects, the things that look like folders in the Solution Explorer aren't actually folders, they are filters. They are UI-only entities that can be used to organize project items in the IDE. They do not in any way reflect the structure or location of items on disk, though. You can add a file from any location on disk to any filter in the solution.
The compiler knows nothing of these filters; it only knows about files as they exist on disk.
In your case, the files are not located in your project folder. You'll either need to:
copy your files into your project folder, then add them to the project from there (right-click on the solution then Add Existing Item), or
add the files from where they are, then add their location to the "Additional Include Directories" property in the project properties.
I do not know of any way to have the IDE automatically move files to the project directory when you copy and paste them into the project. The C++ project system is fundamentally different from the project system used for C# and VB.

Header files not found

I'm adding unit tests (SenTest) to my existing XCode 4.2 project. XCode is complaining that it can't find the required Box2D header files. For instance,
Box2D/Dynamics/b2Fixture.h file not found
The Box2D source files are added to my project under the "libs" group. The header files are found without a problem when building the non-test target. Obviously, I can't add the header files to the test target, but I've added all of Box2D's .cpp files to that target. That just resulted in more of the "Lexical or Preprocessor Issue"s, as above.
How do I tell XCode where to find these header files?
I don't believe you need to specify the folder when referencing a .h file. If only the .h file is added to the project in any group or subgroup XCode is able to find the path when you reference the .h file by the name only.