visual studio community 2022 not building files before debugging - c++

Recently I wiped my pc and reinstalled visual studio community 2022.
Before re-installation, whenever I made some changes in my projects, I would press F5 to enter debug and it would build all files before entering debug mode.
To make it clear, I can manually build the files every time via pressing ctrl+F7, but it is just a work around.
Now, after making changes and pressing F5 it ignores those changes and runs the last build.
I went over the settings and made sure everything matches with another install I have on a second machine.
I found this post the source file is different from when the module was built and went over all suggested solutions - nothing helped.
I tried making a new project with the thought that maybe the project configuration was at fault... no results.
If you want to re-create this behavior, create a new C++ project and 2 files in it (main.cpp, module.cpp).
Here's the code that I used (if you think the code it self causes the issue, please note it):
main.cpp
#include <iostream>
using namespace std;
#include "module.cpp"
int main()
{
handler h;
h.func1();
return 0;
}
module.cpp
#include <iostream>
using namespace std;
class handler
{
public:
void func1()
{
cout << "idk" << endl; // change this line
}
};
Try debugging this setup, for the first time it will build all files since they're new.
Afterwards change the marked line, and debug again (without manually building).
Placing a breakpoint in main.cpp and following into func1() will either work smoothly or result in the behavior I described.
Here are screenshots if anyone needs them.
open file popup
source not found tab

Possible cause 1
I think it's because your file module.cpp is seen as a text file and not as a source file by VS (Visual studio), and therefore VS won't rebuild your project after it is changed.
If it's a source file it should be refered in YourProject.vcxproj as:
<ItemGroup>
<ClCompile Include="module.cpp" />
</ItemGroup>
So, in order to add new source files cleanly you want to use the VS's built-in "add class" (shift+alt+c) and use those files.
In your case, your can repair your project by editing your vcxproj file as in my example.
Possible cause 2
Including directly the cpp file can be problematic: cpp are compiled (as refered in the project file), including it will copy its content, leading to duplicates in the binaries if it's included more than once, so you can't use it several times (but you can include the .h several times, the .cpp being compiled once) ! hence in general, stick to the .h and .cpp pair canon.
Instead you want to have a .h .cpp pair, and put your declaration in .h and your code in either of them, and include the .h in your main source.
<ItemGroup>
<ClCompile Include="module.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="module.h" />
</ItemGroup>
In that case, the binaries were updated when the source were, when I asked to debug (F5).
module.h:
#pragma once
#include <iostream>
using namespace std;
class module
{
public:
void func1()
{
cout << "Now it works !" << endl; // change this line
}
};
module.cpp:
#include "module.h"

There's an option for that (of course!).
You want Menu -> Tools -> Options -> Projects and Solutions -> Build and Run, and then select 'Prompt to build' from the 'On Run, when projects are out of date' dropdown.
Pertinent screenshot:

Related

Added C/C++ file to Eclipse Does not Compile

This has to be some kind of newbie question, but I have not been able to find any explaination.
I am running Ubuntu 18, and need to work with some C/C++ files. I've been using TI's CCS which is eclipse based on Windows for years.
I downloaded the Eclipse installer and ran it setting up for C/C++ developers.
https://www.eclipse.org/downloads/packages/installer
I created a new project. There were several different (unexplained) options such as CDT, MESON, MakeFile, ... I have tried several.
Creating a HelloWorld source file, it compiles and runs fine.
#include <stdio.h>
int main() {
puts("Hello World");
return 0;
}
Okay, so far...
Now I add a new source file. Called "OtherFile.c"
#include <stdio.h>
void OtherFunction() {
puts("Other Hello");
}
And of course, modify the original:
#include <stdio.h>
extern "C" void OtherFunction();
int main() {
puts("Hello World");
OtherFunction();
return 0;
}
When I try to build, it will not compile the new file. And (as expected) it tell me that "OtherFunction" is unresolved.
I have tried multiple project types (CDT, Meson, Makefile) even though there is no explanation of the differences. The newer file will not be compiled.
I tried changing the file extension from c to cpp and back. The newer file will not be compiled.
The TI version of CCS using Eclipse will include a source file when it's in the folder. However, in this environment, I cannot convince Eclipse to compile any other file than the one that was originally created by the new C/C++ project step.
And just as annoying is the fact that I can't right click either file and "Build Selected File". The menu option doesn't even appear.
This did not work for me:
eclipse c/c++ CDT build just one file
Can someone advice how to convince Eclipse to compile additional files?
TIA.
EDIT:
I can't upload here, so I just created something on GitHub.
These are two of the samples where I added a second file, and it ignores it.
https://github.com/scotty2541/EclipseExample
In all the other things I've done in Eclipse, it simply uses a default "recipe" like make does to compile the file.
If there is some way to manually tell Eclipse about it, that isn't explained anywhere I've been able to find. And seems to defeat the purpose of the IDE's behavior.
I was able to get it to behave as expected: By choosing a CDT managed build system, when adding a file to the project, it compiles it using the default recipe
Then, there is a setting which causes it to run the "builder" after a clean.
When I added the file as described originally, I also had to do a "clean" in order for the environment to include the additional file.

Visual Studio throwing LNK2019 and solution missing headers/source files

-- Updates --
Adding the source file settings.cpp directly to the project via Add -> Existing Item resolved the LNK2019 (as I suspected it couldnt find it).
-- Updated Question--
How to specify a source file directory without having to add all the files within it manually as described in the update above? This is clearly achievable for the header files (as described below by adding the directory to the settings, is this not the same for source files?
-- Original Question --
I'm working on replicating a project from CPython into C++ as a challenge to learn more C++ but I can't seem to get the environment established so that I can compile a test run. When I build the solution, it throws a LNK2019 which I know has something to do with the Linker unable to locate the symbols. I have read through numerous solutions on SO that say to updated the properties with the directories of the files, but they do not resolve the issue.
The issues currently stand at:
Some of the headers from other directories show in the explorer, but some do not, why?
The source files are not found and therefore LNK2019 is thrown, cannot resolve, how to?
Here is the layout of my project:
/root
/proj-cmd
/src/main/cpp
/proj/cmd
-> main.cpp
/proj-core
/src/main/cpp/
/proj/cmd
-> command.h
-> base_command.h
-> base_command.cpp
/proj/utils
-> settings.h
-> settings.cpp
The content of main.cpp for testing of environment:
// astro
#include <astro/core/util/settings.h>
// stdlib
#include <exception>
#include <string>
#include <iostream>
using namespace std;
// astro entry point
int
main(int argc, char *argv[])
{
if (conf().hasKey("APP_CWD"))
{
cout << "APP_CWD is: " << conf().getKey("APP_CWD") << endl;
}
else
{
cout << "APP_CWD was not found" << endl;
}
}
In order for #include <astro/core/util/settings.h> to work, I updated the include directories in the properties:
However, in the explorer only command.h and settings.h are shown, not base_command.h:
Additionally, the base_command.cpp and settings.cpp do not display in the source files either, so I updated (similar to the include directories) the source directories:
That takes care of the first issue I am noticing, but now onto LNK2019. I believe this is a related result of the former problem, in that the source files are unknown:
So following many other SO posts, I tried to update the Linker settings without success:
I'm not very familiar with the Visual Studio 2017 environment, so if somebody could provide input as to how to configure these settings so that I can compile I'd appreciate this.
You need to add all .cpp files to your project as Existing Items. Just being in a directory is not sufficient for the IDE to know to compile those files. Headers are found by directory via #include, but you should still add them to your project as Existing Items to make it easier to navigate them in the tree view.
This is why you are getting linker errors: the code in settings.cpp and base_command.cpp are never getting built.
See Microsoft Docs

Visual Studio 2019 not building all cpp files when a header file changes

I have been trying to figure out why visual studio has not been recompiling all the compilation units affected by a header file change in my Library project.
Lets say I have the following header file TestA.h:
class TestA
{
public:
int GetNumber() { return 0 };
std::string GetString(); // implemented in TestA.cpp
}
Lets say I then have 3 .cpp files TestA.cpp TestB.cpp TestC.cpp
TestA.cpp Only contains the function implementations for TestA
However both TestB.cpp and TestC.cpp follow something of the sort:
#include "TestA.h"
....
TestA* aObject = new TestA();
int aNumber = aObject->GetNumber();
...
This is where the problem now lays, if I change the function GetNumber in TestA.h to GetNumber1 Visual studio and msbuild command line will only build TestA.cpp and it will succeed in compiling. It will then generate the .lib using the now stale .obj files. If I however rebuild then I get the errors that I do expect. I have tried enabling/disabling Incremental linking, minimal rebuilding, Managed Incremental Build.
I can achieve the desired behavior by deleting the .obj and the .tlog files from my intermediate directory.
Any help is appreciated
Thanks
Sumeth
So after exhausting all my ideas I decided to try and just recreate the project. And that fixed it! Not sure what exactly happened but deleting all the vcxproj files and recreating the vcxproj from scratch fixed my issue. Not sure what exactly changed but that seemed to solve it

error in code of timetable generator using genetic algorithm [duplicate]

When I build my c++ solution in Visual Studio it complains that the xxxxx.pch file is missing. Is there a setting I am missing to get the pre-compiled headers back?
here is the exact error for completeness:
Error 1 fatal error C1083: Cannot open precompiled header file: 'Debug\xxxxx.pch': No such file or directory
NOTE: Later versions of the IDE may use "pch" rather than "stdafx" in the default names for related files. It may be necessary to substitute pch for stdafx in the instructions below. I apologize. It's not my fault.
Right-click on your project in the Solution Explorer.
Click Properties at the bottom of the drop-down menu.
At the top left of the Properties Pages,
select All Configurations from the drop-down menu.
Open the C/C++ tree and select Precompiled Headers
Precompiled Header: Select Use (/Yu)
Fill in the Precompiled Header File field. Standard is stdafx.h
Click Okay
If you do not have stdafx.h in your Header Files put it there. Edit
it to #include all the headers you want precompiled.
Put a file named stdafx.cpp into your project. Put #include "stdafx.h"
at the top of it, and nothing else.
Right-click on stdafx.cpp in Solution Explorer. Select Properties
and All configurations again as in step 4 ...
... but this time select Precompiled Header Create (/Yc). This will only
bind to the one file stdafx.cpp.
Put #include "stdafx.h" at the very top of all your source files.
Lucky 13. Cross your fingers and hit Build.
Precompiled Header (pch) use is a two-step process.
In step one, you compile a stub file (In VS200x it's usually called stdafx.cpp. Newer versions use pch.cpp.). This stub file indirectly includes only the headers you want precompiled. Typically, one small header (usually stdafx.h or pch.hpp) lists standard headers such as <iostream> and <string>, and this is then included in the stub file. Compiling this creates the .pch file.
In step 2, your actual source code includes the same small header from step 1 as the first header. The compiler, when it encounters this special header, reads the corresponding .pch file instead. That means it doesn't have to (re)compile those standard headers every time.
In your case, it seems step 1 fails. Is the stub file still present? In your case, that would probably be xxxxx.cpp. It must be a file that's compiled with /Yc:xxxxx.pch, since that's the compiler flag to indicate it's step 1 of the PCH process. If xxxxx.cpp is present, and is such a stub file, then it's probably missing its /Yc: compiler option.
Fix:
Make sure you have xxxxx.cpp in your project
Compile xxxxx.cpp with /Yc flag (Create Precompiled Header)
(right click on xxxxx.cpp -> properties -> Precompiled Headers -> create)
Compile all other files with /Yu flag (Use Precompiled Header)
(right click on project -> properties -> Precompiled Headers -> use)
Right click to the project and select the property menu item
goto C/C++ -> Precompiled Headers
Select Not Using Precompiled Headers
Yes it can be eliminated with the /Yc options like others have pointed out but most likely you wouldn't need to touch it to fix it. Why are you getting this error in the first place without changing any settings? You might have 'cleaned' the project and than try to compile a single cpp file. You would get this error in that case because the precompiler header is now missing. Just build the whole project (even if unsuccessful) and than build any single cpp file and you won't get this error.
In case this is happening to you on a server build (AppCenter) and yo uaer using CocoaPods ensure that your Podfile is checked in.
AppCenter only runs the "pod install" command if it finds a Pofile and it DOES NOT find the PODS folder on the files.
I had the folder checked-in, but because git automatically ignores .pch files (check you .gitignore to veryfy this), my .pch weren'nt being checked in.
I sorted my issue by forcing the .pch files to check it, but Deleting the PODS folder should work too, since Appcenter will run the pod install command in that case.
Hoppefully this helps somebody.
VS screwed (mine is 2019 ;( ).
Go ahead and choose "not using precompiled headers" as other guys are pointing out then open the project file (vcxproj) with any text editor, and delete the outlined two entries in two places. Enjoy cleaning up the mess!
As a matter of fact, the 'pch.h' entry in the vcxproj file you see it below, you will ever find it in VS properties' interfaces.
Try Build > Clean Solution, then Build > Build Solution. This works for me.
I know this topic is very old, but I was dealing with this in VS2015 recently and what helped was to deleted the build folders and re-build it. This may have happen due to trying to close the program or a program halting/freezing VS while building.
I was searching for the iOS PCH file having the same problem, if you got here like me too, the solution that I've found is by clearing derived data; Close Simulator(s), go to xCode prefs -> locations -> go to the derived data file path, close xCode, delete the files in the derived data folder, re launch and cheers :)
I managed to create this problem for myself because I wanted to use a pch.h and pch.cpp file from different directories. So, I deleted the two files from my project and then added them as existing files from somewhere else. Big mistake as precompiled header files can no longer be found.
There is no way that I can find to fix the problem from the Visual Studio 2019 UI. You must edit the project file and make sure the following look like this:
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
I had same issue, and I managed to solve it like this:
ERROR :
fatal error C1083: Cannot open precompiled header file : "Debug\myProj.pch". No such file or directory
first one is when I had an error,
and changed it like a second picture
make (/Yx)
myProj.h
In my case, it was necessary to select Create (/Yu), instead of the standard Use (/Yu)
to
If everything is right, but this mistake is present, it need check next section in ****.vcxproj file:
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition=
In my case it there was an incorrect name of a configuration: only first word.

Can I set a compile time flag to include a header?

I have a configuration class that I would like to use for a variety of builds. The class itself changes between builds, but the class name remains the same, as does the header file name.
The separate versions of this class are held in separate subfolders.
ex.
main/config.h
main/config.cpp
secondary/config.h
secondary/config.cpp
Is there a good way to, through a compile-time flag or command line option, have the build determine which header/cpp to use? I have quite a few configurations already, and expect to have many more in the future. I would like to avoid a long list of #ifdef/#elif/#elif/etc..
edit: I would like to avoid having separate builds, and would like to avoid using #defines throughout the code. I'm sorry if I didn't make that clear before! >_<
Depending on what build system you are using you would create a variable that points to the main or secondary path. This variable is then used to append to the INCLUDE path so all of your sources can just #include "config.h" when they need access to config. In your Makefile (or equivalent) you will need to add the $CONFIGPATH/config.cpp to your sources to build.
MSBuild
Update source file paths:
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="$(ConfigToUse)/config.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(ConfigToUse)/config.h" />
</ItemGroup>
And the include path:
<PropertyGroup>
<ConfigurationType>Application</ConfigurationType>
<ShowAllFiles>false</ShowAllFiles>
<IncludePath>...;$(ConfigToUse);</IncludePath>
</PropertyGroup>
Then call msbuild build.xml /p:ConfigToUse=PathToConfig
The #include directive doesn't really care about content.
You can just as easily have a stub class that you use in your project:
stub.cpp
#ifdef BUILD1
#include "main/realimpl1.cpp"
#else
#include "secondary/realimpl2.cpp"
#endif
And, of course, you can do the same thing with headers if necessary.
You can put into your header/cpp guards like this:
#ifdef OPTION_A
...
#endif
You can use compile-time #define such as
#define USE_BUILD_X 1
//#define USE_BUILD_Y 1
...
...
#ifdef (USE_BUILD_X)
#include "mainheader.h"
#elif (USE_BUILD_Y)
#include "secondheader.h"
#endif
I suppose the good choise is to use several makefiles or whatever you use. One for each configuration. Do not make your source files unreadable.
You can place common files in the Common directory, and other files in separate directories - one directory for each configuration.
In Visual Studio (if that's your IDE) you can have multiple "configurations" (by default Debug and Release), and it's possible to have certain files not included in each build. You could make configuration "Debug main" which excludes secondary/config.cpp, and configuration "Debug second" which excludes main/config.cpp.
If you're using not using Visual Studio, I believe there's a way to do something similar with make files.
You can just add the relevant directory to the front of the compiler's include path.
You can change the compiler's include path via some compiler option (it depends on the compiler).
For the .cpp file it's the same. Just have a .cpp file in your ordinary source tree, that includes that .cpp file via a #include directive.
Cheers & hth.,