netbeans can't find C++ header file in same directory - c++

In one particular cpp file (abc.cpp), when I ask to navigate "to Declaration/Definition", it says "cannot open element "abc.h"". This functionality works for other header files. This in itself is not a big problem, but it also means that auto-complete and syntax highlighting doesn't work for this file.
Some extra info:
The header file is in the same directory as the cpp file and both are included in the active netbeans project
I was able to enter the header file name with auto-complete, i.e. #include "ab<ctrl-space>"
clicking the "Go to header/source" button works both ways for this cpp/h pair.
right-clicking on the class name in the header file, and then selecting "go to source" brings me to the cpp file, as usual.
in other cpp files the connection to the header file is working fine, as is autocomplete & syntax highlighting
netbeans has a green square in the top right of the header file window, indicating "no errors"
I have tried deleting my cache as explained here
I'm using netbeans v8.0.2 on OpenSuse 13.2

Here are some ideas:
Sometimes the Code Assistance is not as good for projects that Netbeans didn't create from scratch. If it is not a complicated Makefile, it might be worth it to create a new project with the C/C++ Application type and copy over and then add each source and header file.
The code assistance depends on analyzing he log from the build each time, so sometimes just rebuilding the project will fix the code assistance.
There are a number of options if you right click the project under the code assistance sub-menu.
Edit the Makefile to make sure this file is being compiled in the same way as the other files that work. It may be getting compiled with different options because it was added later and therefore not providing the same info for code assistance. You will need to rebuild after making these changes for them to have an effect.

Related

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.

Eclipse-PDT: change the include folders

I'm using Eclipse-PDT with a kind of cross-gcc(gcc customized to compile the program) in Linux. The problem is, when I included a header file in the source code, e.g. #include <stdbool.h>, Eclipse would try to find the file under the folder /usr/lib/gcc/x86_64-linux-gnu/4.9/include, but I hope Eclipse to find the customized header file, which is under another foler: /home/user/crossgcc/lib/gcc/i686-elf/4.9.2/include. I've tried to change a lot of settings, but it didn't work still. How to do the trick?
I found it. Right click the project and select Properties, delete the original ones and add yours.

Netbeans-specific C++ error "Undefined reference to XXX" - (Solution posted)

I wrangled with this problem for a good 5 or 6 hours, pulling my hair out until I finally found a solution. I wanted to post this (not sure if there is a specific place to post solutions to unasked questions) as a solution for others who may run into the same difficulty.
I am coding a C++ project in Netbeans 7.1.1 running on Linux Mint Lisa, and kept on getting an "Undefined reference to XXX" error when trying to use a static variable. Coming from a background in C# and thus not very familiar with header files and the like, I searched for hours expecting to find a problem with the way I declared my variable or my class. I couldn't find anything...
Go to your nbproject folder in the files explorer in Netbeans. Open up the configurations.xml file. Scroll down to the logicalFolder tag with the attribute name="SourceFiles". Make sure that all your .h and .cpp files are included as itemPath tags within the logicalFolder tag! Netbeans correctly added some, but not all of my cpp and h files, hence the problem. Not sure why there would be this inconsistency, as all class and header files were added through the same Netbeans wizard. Anyways, with the configurations file changed, the makefiles were then updated and written correctly on the next build and all functioned as expected. Hope this can help somebody!
Just to add more details to Levi's answer (for anyone newish to this like me), click the "Files" tab on the left of the screen, where your list of files and folders is. This gives you a different list of files than the "Project" tab does.
Expand your project folder if it isn't expanded already. One of the resulting folders is "nbproject." Expand the folder. This is where configurations.xml is.
In short:
Files tab > nbproject > configuration.xml
However, adding the .h files to configuration.xml did not work for me.
In my case, I only had to include the source files as #include lines within other source files to make it work ... I could compile the individual source files without error, but the project was another story. Once I added the #include lines in other source files that required certain methods to be found, then it all worked as planned. Hope this helps someone.

Precompiled header and Visual Studio

Is there a way to set Visual Studio solution parameters so it just create precompiled headers without building whole solution.
Specifically, it is a huge c++ solution with many projects in itself.
Thank you.
Only select the pch creator source file (usually stdafx.cpp), and compile that (Ctrl-F7, or right-click it and select 'Compile')
More info since it doesn't seem to be working for you:
In every project that uses a precompiled header, there is one source file that is used to create the pch file, and the rest only use the pch file. This source file usually only consists of one line:
#include "StdAfx.h"
"Stdafx.h" is the default precompiled header file name in Visual C++, it could be something else for you. If you compile "StdAfx.cpp" by itself, that generates a file with the name "Your_Project_Name.pch" (again, that's only the default). You should see it in the intermediate directory, the same one where all the obj files are. This is the precompiled header. If you did like I said, and selected 'Compile' and not 'Build', then no other files will be compiled, and no linking will take place.
If that still does not solve your problem, then I have no idea what you are asking.
Here's an article describing the benefits and how to set it up.
If you have larger projects, it is definitely worth the few clicks and the extra disk space.
The article says you need to add stdafx.h to all sources and headers; it's not correct. It's sufficient to add to sources - make sure it's the first line though as everything before it will be ignored.
The article does not mention it, but you'll be getting errors from sources that do not include the stdafx.h. You have a choice to resolve this error: you either add it, or exclude the source(s) from the precompilation process.
To exclude source files:
select source file(s) in the solution explorer; yes you can select more at once,
right click on the highlighted source file,
click properties from the pop-up menu,
select 'All configurations' from the combo-box on top,
under C-C++ configuration click 'Precompiled headers',
on the right-hand side select 'Not using precompiled headers',
click apply,
click ok.
Enjoy your new builds in a few seconds from here on (the first build will take longer).
If you right-click any Cpp files except stdafx.cpp from your project and set Excluded from build to Yes, it will only generate the precompiled header.
You can achieve the same result through the command line or if you create new project containing only your stdafx.cpp

About stdafx.h missing in my compiler(mingw32 on windows)

I just have a quick question. I've noticed that I don't have stdafx.h in my compiler(mingw32 on windows)
Am I supposed to have it? Or maybe is there a way to get around it?
Thanks for reading
EDIT: ok here is my current build log once I took out ALL of the includes of stdafx.h
http://pastebin.com/bczLr8xY
Read this wikipedia article. The paragraph I linked and the paragraph below it (mingw32 uses GCC).
http://en.wikipedia.org/wiki/Precompiled_header#stdafx.h
Since stdafx.h contains the most common headers I would remove every instance of #include stdafx.h and try to compile. If you get compile errors that a certain function is missing, add the appropriate header. Rinse and repeat.
No. Stdafx.h is created with MSVC++. It usually contains most common headers files. And Stdafx.h is included in every .cpp file in the beginning. It's precompiled header (if you've chosen so in the settings) created by MSVC++.
To all:
Using the pre compiled header file stdafx.h in the visual C++ always creates one or the other problem n case u have created a "Windows Console App" from Visual C++.
The Solution to it is that, just create "Empty Project", rather than the pre-compiled windows console application. After creating the Empty project, create the source File. Write the business logic and add all the required resource and header files. Keep the external dependency files in the same directory in which your source code is: e.g., C:\Users\John\Documents\Visual Studio 2010\Projects\xyz.cpp\xyz.cpp.
Finally add the source file to the global scope, that is add it to the "Empty Project" created already. It can be done by Clicking "File" on Visual Studio prompt and select the option of adding the source file to the project.
Thanks and Regards:
Rouf Khan