Why Xcode gives error 'string' file not found? - c++

I am trying to interop with basic C++ method in Swift. But Xcode gives me an error when I build the project;
'string' file not found
I have researched this issue on StackOverflow but every solution and answers didn't solve my problem. And I started to think that what if I use libstdc++ rather than libc++. Then I found that libstdc++ was removed with Xcode 10. I found the files on Github and apply it to appropriate file directories. And that did not do much either. Did you guys encounter with this kind of problem before?
This is my header file.
#ifndef CPPTest_hpp
#define CPPTest_hpp
#include <string>
std::string saySomething();
#ifdef __cplusplus
extern "C" {
#endif
int returnNumber();
#ifdef __cplusplus
}
#endif
#endif CPPTest_hpp
This is the cpp file.
#include "CPPTest.hpp"
std::string saySomething()
{
return "Welcome to C++";
}
int returnNumber()
{
return 10;
}
This is where I expose header to Swift
#include "CPPTest.hpp"
After all, When I build the project. It gives me an error like;
'string' file not found
Why does this error occur?
By the way, it works when I use integer as return type and its appropriate implementation in cpp file.

Related

Not able to include standard c++ library headers in vs code esp-idf extension

I am not able to include some standard libraries:
#include <stdio.h> // <= this works
// #include <thread> // <= error: "No such file or directory"
// #include <algorithm> // <= error: "No such file or directory"
void app_main(void)
{
}
The error is generated by compiler xtensa-esp32-elf-gcc.exe. There is no error from C/C++ Intellisense. And I am able to find the necessary header by pressing F12. They are placed on the path C:\Espressif\tools\xtensa-esp32-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\8.4.0. There is no such path in the compiler -I arguments.
How can I deal with this error?
Thread and algorithm are both part of C++ standard library, not C standard library. I've renamed the source file to .cpp (CMakeLists.txt was updated by VSCode automatically). Then I added:
extern "C" {
void app_main();
}
And it works. Hope it helps somebody.

Error in creating a class header file

I am attempting to create a simple class header file. I have done the following so far:
#ifndef RECORD_H
#define RECORD_H
class Records{
int idNumber;
int serialNumber;
public:
Records();
};
#endif
However, I am getting the following error:
[Error] unknown type name 'class'
What am I doing wrong?
You're probably compiling it as C rather than C++. That is why you are getting that error..
Make sure your source file has a .cpp extension.
As lakesh said, you may to compiling as C rather than C++. Most compiler use the file extension to determine how to compile the file (use .cpp instead of .c)
If that's not it, then the problem would probably be in the a different file loaaded before this.
// Record.cpp
#include "badfile.h" // error in here
#include "record.h" // error showing up here.

Chain of C libraries into C++

I have a very trivial problem including a chain of C libraries into a C++ main project. I've experience with C but it's the first time that I'm programming in C++.
The structure of the project is a single folder with inside:
main.cpp
Mylib_1.c
Mylib_1.h
Mylib_2.c
Mylib_2.h
main calls -> Mylib_1.h that calls -> My_lib2.h
//main.cpp
#include "Mylib_1.h"
//Mylib_1.h
#include "Mylib_2.h"
main contains both Mylib_1 and Mylib_2 functions and typedef structs
Mylib_1 uses typedef structs and functions of Mylib_2
Everything inside each Mylib_x.h is wrapped between extern "C", like this:
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
#ifdef __cplusplus
extern "C" {
#endif
mycode
#ifdef __cplusplus
}
#endif
#endif
But when I try to compile it with eclipse kepler on Ubuntu 12.04 x64, I get:
Mylib_1.h error: Mylib_2_type_t does not name a type
main.cpp error: Mylib_2_function1 was not declared in this scope
...
Only the above sections are marked as error in eclipse, the header looks included fine.
Furthermore according to eclipse, the __cplusplus flag is false into Mylib_2.h but true into Mylib_1.h
Thinking of some eclipse error, I've tried to manually build the project via g++ (v4.6.3) but I got the same exact problem when I've tried to link the libraries .o with the main.cpp
Seems stupid but I can't figure out what could it be. Any suggestion?
Thank you
Have you checked that your lines
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
are really different for the two files,
e.g. _MYLIB1_H and _MYLIB2_H?

Visual Studio not finding included definition

I'm pretty rusty in C++, so forgive me for any stupid comments/questions. Right now I'm working in Microsoft Visual C++ 2010 Express. I have two files - a source and a header - and VS is recognizing the header file when I include it, but it cannot find any definitions from within the header file. It shows me 'Error: identifier "RAW_PACKET_SIZE" is undefined'. The code was provided as a sample to work with a device's API, so it should work. I'm assuming the problem is with the VS setup. Here's some intro code form each:
recorder.cpp
#include <vector>
#include "APIW32.h"
#pragma comment(lib,"APIW32.lib")
int devID;
float* buf = new float[RAW_PACKET_SIZE]; // error is here, at 'RAW_PACKET_SIZE'
APIW32.h
#pragma once
#ifdef EXPORTS
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
#define MIN_BW 0.301003456
#define MAX_BW 10100000
#define RAW_PACKET_SIZE 299008
UPDATE:
It appears that the error was only appearing in Intellisense, and not as an actual build error. Moral of the story - Intellisense is not always right!
Try float* buf = new float[RAW_PACKET_SIZE];

PCH Warning: header stop cannot be in a macro or #if block - Visual C++ 2010 Express SP1

This is pasted from a website, which presumably was working. I did some googling and found that the issue I have now is a result of Visual C++ 2010 SP1, which I downloaded today, and is now giving me this error:
PCH Warning: header stop cannot be in a macro or #if block.
Hopefully someone will be able to help me with this!
#ifndef APP_STATE_H
#define APP_STATE_H
#include "Framework.h"
class AppState; //this line is giving me the error
//define two classes
#endif
Framework.h:
#ifndef OGRE_FRAMEWORK_H
#define OGRE_FRAMEWORK_H
#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreOverlay.h>
#include <OgreOverlayElement.h>
#include <OgreOverlayManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>
class OgreFramework : public Ogre::Singleton<OgreFramework>,OIS::KeyListener,OIS::MouseListener{
public:
OgreFramework();
~OgreFramework();
bool initOgre(Ogre::String wndTitle, OIS::KeyListener *pKeyListener = 0, OIS::MouseListener *pMouseListener = 0);
void updateOgre(double timeSinceLastFrame);
//OIS
bool keyPressed(const OIS::KeyEvent &keyEventRef);
bool keyReleased(const OIS::KeyEvent &keyEventRef);
bool mouseMoved(const OIS::MouseEvent &evt);
bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id);
Ogre::Root* mRoot;
Ogre::RenderWindow* mRenderWnd;
Ogre::Viewport* mViewport;
Ogre::Log* mLog;
Ogre::Timer* mTimer;
//OIS
OIS::InputManager* mInputMgr;
OIS::Keyboard* mKeyboard;
OIS::Mouse* mMouse;
private:
OgreFramework(const OgreFramework&);
OgreFramework& operator= (const OgreFramework&);
};
#endif
I had the same issue and was looking for a solution. Following worked for me:
Add #pragma once at the start of the file (even before the #ifndef APP_STATE_H header guard)
You probably used a project template to get started and threw away the pre-generated source code files. Those project templates like to turn on precompiled headers because it is such a time-saver. Right-click your project in the Solution Explorer window, Properties, C/C++, Precompiled Headers. Change the "Precompiled Header" setting to "Not Using".
1.Close the Project.
2.Reopen the project,and all ok.
this is my expeirence.
move the #include statements outside the #if #end block
Rebuilding IntelliSense database solves the problem.
Close Visual Studio
Delete [SolutionName].sdf
Delete DllWrappers.opensdf
Delete ipch folder
Open Visual Studio
I had the same problem. My solution was to add a missing ';' at the end of a class definition. Although this does not seem to apply to your problem, others who come here with the same error might find this helpful.
This is probable a day late and a dollar short but I had the same error when I accidentally put my header file in a .cpp file instead of a .h file. I will post it though in case it can help someone.
I just added a referenct to the header file (#include "header.h") and it helped.
I found that my .h file was actually being treated as a .cpp file! Right click on the file in the Solution Explorer > All Configurations > Item Type: C/C++ header
Make sure the item type is not C/C++ compiler or other.
Once you add a .cpp file and have it include the header this error should go away. I've read some where else this is a bug.
I use Visual Studio to edit Linux projects. For me, the issue was present when I include string.h in my precompiled header file. It was caused by lines that have an __asm statement, for example:
__THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));
The solution was to define the following macro under Project Properties, Configuration Properties, C/C++, Preprocessor, Preprocessor Defines:
__asm(x)=
In my case I was wrapping the whole .cpp file inside of #ifdef directive and getting this error.
The issue is, when using precompiled header, your standard precompiled header include needs to be outside of that!
For example:
#include "pch.h" // <- outside of any #if pragma directive
#ifdef EXAMPLE_DIRECTIVE
#include "another_header.h" // <- any other headers can be included inside
class AppState {}; // etc, your code here
#endif
Note that this issue can likely be also caused by some header you're including, which includes PCH header itself. If that's the case, just copy the import of PCH header from that file to the top of the file you're getting the error in, above the #if block.