Can't open header file in C++ - c++

I have a header file called simpio.h, which is in the same folder as the file which is including it in its header. However, I keep on getting the error "Cannot open include file: 'simpio.h': No such file or directory." I am using Visual C++ 2008 Express Edition. Help would be appreciated.
Thanks

You need to use double quotes:
#include "simpio.h"

You have to know that you should use <> when you are trying to include a standard library header or when you want to include a file for which the path is contained in the Additional include directories option.
You have to use "" when you whant to include a file who doesn't satified the previous explanation, let's say that it is almost always file specific to your project.
Some example :
#include <iostream> // library file
#include <boost/thread.hpp> // there is "C:\SDKS\boost in my Additional include directories
#include "MyHeader.h" // Local file, at the same place of my .vcproj
#include "Header/AnotherHeader.h" // Local file in a folder named "Header"
In your case, we can think that you are in the second case. You just have to do :
#include "simpio.h"
Or if your file is in another folder :
#include "SomeFolders/simpio.h"

Related

Visual studio code rename header

Anyone know how to rename a header file in vscode so that all includes of the header file will be updated accordingly?
For example, if I rename Header0.h to Header1.h, all includes will change from
#include "Header0.h"
to
#include "Header1.h"
Use of extensions is also welcome

Cmake include path for system and program files

I'm wondering if there is a possibility to configure cmake, that it's possible to make a difference if headerfiles are included with #include<...> or #include "..."?
Lets assume that I have a project like this:
src
foo.h
foo.c
lib
foo.h
foo.c
How can be achieved to add the file src/foo.h by writting #include "foo.h" and if the headerfile is included with #include <foo.h> the file at location lib/foo.h is loaded?
It always load the files from the first include_directories() command
include_directories(src)
include_directories(lib)
EDIT
My current problem inside my project:
I have a file named string.h. Which contains some function for custom text handling. Now I have the problem that if I write #include <string.h> my custom file is loaded, but I expect the system file string.h. I don't want to rename my custom file nor I want to move it inside a folder to include it like #include "custum/string.h"
All I want is:
#include <string.h> // load file from system library
#include "string.h" // load my custom file
I used it inside IDE Keil like this ways, but I don't know if it can be done with cmake
Change include order
include_directories(lib)
include_directories(src)
You will need to place your whole code in the src. In this case #include "foo.h" starts search files in the same directory where current file is parsed. #include <foo.h>" starts search in include directories in the order of include_directories.
BTW it is a bad practice to do such things. Your files should be searched first and then lib files. Better way is to do like
include_directories(src)
include_directories(parent_of_lib)
Use #include "foo.h" or #include <foo.h> for your own included files and #include <lib/foo.h> for third-party included files.

Boost: how to handle its nested include files?

I have a Hello World program that uses
#include <boost/process.hpp>
I set the Additional Include Directories and this file is found. Its first include file is
#include <boost/process/args.hpp>
and that is found O.K. It in turn has an include file
#include <include/boost/process/detail/basic_cmd.hpp>
that throws an error
C1083: Cannot open include file: 'include/boost/process/detail/basic_cmd.hpp'
which is correct in its way - there is no lower level include file!
Manually changing this to
#include <boost/process/detail/basic_cmd.hpp>
allows that file to be found, but its child then throws the same type of error. What Visual Studio setting handles these nested includes automatically?
I too had include-file trouble with with the .zip file download.
boost/static_assert.hpp includes itself
Install boost using the .7z archive instead of the .zip. (Advice applies to 1.65 only.)

Project "Additional include directory" being ignored

I'm including files from another project in the same solution - it's all worked fine in it's current configuration but I added a new method, and now it can't find any of the include files.
Here's the header file dems_gridmanagement.h:
#ifndef dems_gridmanager_h
#define dems_gridmanager_h 1
//## begin module%500EBD96007C.includes preserve=yes
#pragma once
#include <QObject>
#include <QTime>
#include <QString>
#include "common_StringUtility.h"
#include "common_SharedMemory.h"
#include "common_QtThread.h"
#include "common_QtThreadable.h"
#include "sma_Maps.h"
#include "dems_Types.h"
...
#include "MQTTs_UDP_Multicast.h"
...
And in the additional include directory:
.\GeneratedFiles;$(QTDIR)\include;.\GeneratedFiles\$(Configuration);$(QTDIR)\include\qtmain;$(QTDIR)\include\QtCore;$(QTDIR)\include\QtSql;.\;..\Common;..\DemsCommon;..\Ethernet;..\SMA;..\ModbusTCPIP;..\MQTTs;%(AdditionalIncludeDirectories)
Crucially, it includes the "..\SMA", which should allow the "sma_Maps.h" to be found, but I'm getting:
Error 3 error C1083: Cannot open include file: 'sma_Maps.h': No such file or directory c:\users...\solution\projects\DemsGridManagement\dems_GridManager.h 33
I tried changing it to:
#include "../SMA/sma_Maps.h"
which get's rid of the error, but then introduces a new error:
Error 59 error C1083: Cannot open include file: 'MQTTs_UDP_Multicast.h': No such file or directory c:\users...\solution\projects\DemsGridManagement\dems_GridManager.h 39
And also a similar error for an include in a project file that is in a completely different project within the solution, and I haven't even opened or changed anything in.
For some reason, it's like the solution is just selectively ignoring the include directory information with some of the project files, but not others (QT includes work fine, so do the "common" project includes...
Does anyone have any ideas what could be causing this? It was all working fine yesterday!
Edit: Narrowed it down. I added a reference to GridManager from another project:
#include "../DemsGridManagement/dems_gridmanager.h"
If I remove this, there's no problems in the dems_gridmanager.h file. If it's here, the includes in gridManager go crazy and break.
When including with #include "filename" it always search the same directory as the current file. To get the files you have included in "additional include directories" you should include with #include <filename>

Importing Functions from a different C file

I want to import functions from another file in Microsoft Visual C++ 6.0. How can i do this? I have tried with this as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#import <functions.cpp>
where functions.cpp is the file name from where I want to import the functions. But this gives an error: F:\CC++\Term Project\Dos Plotter\Functiom Plotter.cpp(6) : fatal error C1083: Cannot open type library file: 'Functions.cpp': No such file or directory
How can I solve this problem?
The #import directive is used with type libraries, often COM or .Net, not C++ source files. For complete details, see the MSDN page.
In order to include C++ functions from another file, you typically want to use the #include directive (details). This includes the code from the given file during compilation. Most often, you should include a header containing the function prototypes; it is possible to include code files, but not commonly needed or always safe.
To do this, you should provide two files, a header and a source file, for your functions.
The header will read something like:
#pragma once
void Function(int arg);
and the source:
#include "functions.hpp"
void Function(int arg) { ++arg; }
To use this in another file, you do:
#include "functions.hpp"
void OtherFunction()
{
Function(2);
}
You should also note that a header should typically be included only once. The MSVC-standard method of guaranteeing this is to add #pragma once to the beginning.
Edit: and to address the specific error you've posted, which applies to both #import and #include, the file you're attempting to include must be somewhere within the compiler's search path. In Visual Studio, you should add the necessary path to the project includes (this varies by version, but is typically under project properties -> compiler).
1) Did you mean functions.hpp? C/cpp files should not be #included unless you know very well what you're doing.
2) Add the location of the file to the custom include path in the project properties, or use the include "foo" format instead of include <foo>
3) Import is undefined in C. You need to separate prototypes and implementations, include-guard the prototypes file, and #include the prototypes file.
having the file functions.cpp on the same dir, use include "functions.cpp" instead
Name the file imported-function.hpp, and make sure that it is in the same dir. Or, you could link it to
Linux: /home/uname/appfolder/imported-function.hpp
Windows: C:\Username\uname\appfolder\imported-function.hpp
ChromeOS: /home/chronos/u-4e4342ea6b3b92244e7d4753922f0dc7125f4a1d/MyFiles/appfolder/imported-function.hpp