C++ components

profDev
Posts: 3
Joined: Thu Sep 19, 2019 10:33 am

C++ components

Postby profDev » Thu Sep 19, 2019 11:01 am

I found an nice esp32 library on https://github.com/fhessel/esp32_https_server


it is also using the Arduino librari.
As I prefered using real esp libraries without arduino, i decided to change the code to make it working without include arduino.h.

The library is written in C++ and i have troubles compiling it when the base program is in C.

Code: Select all

error: 'string' in namespace 'std' does not name a type
   HTTPHeader(const std::string &name, const std::string &value);
  

The code causing this error is:

Code: Select all

#ifndef SRC_HTTPHEADER_HPP_
#define SRC_HTTPHEADER_HPP_

#include <string.h>

namespace httpsserver {

/**
 * \brief Represents a single name/value pair of an HTTP header
 */
class HTTPHeader {
public:
  HTTPHeader(const std::string &name, const std::string &value);
  virtual ~HTTPHeader();
  const std::string _name;
  const std::string _value;
  std::string print();
};

} /* namespace httpsserver */

#endif /* SRC_HTTPHEADER_HPP_ */


I have seen there where some troubles with include string a lot of time ago, but the solution was upgrading the source.

git describe
v4.1-dev-256-g9f145ff16

xtensa-esp32-elf-cc --version
xtensa-esp32-elf-cc (crosstool-NG esp32-2019r1) 8.2.0

Can I include c++ components to c programs? It compiles when the program is c++.
Why it doesn't recongnise this include?

davidzuhn
Posts: 17
Joined: Fri Sep 20, 2019 1:50 am

Re: C++ components

Postby davidzuhn » Fri Sep 20, 2019 1:53 am

<string.h> is the C library header file for functions like strcmp.

<string> is the C++ library header file which contains the std::string class declaration.

You need to change <string.h> to <string>.

profDev
Posts: 3
Joined: Thu Sep 19, 2019 10:33 am

Re: C++ components

Postby profDev » Fri Sep 20, 2019 5:30 am

Yes, but changing this results in an more confusing error:

Code: Select all

HTTPHeaders.hpp:8:10: fatal error: vector: No such file or directory
 #include <vector>

fhessel
Posts: 1
Joined: Wed Sep 25, 2019 7:26 pm

Re: C++ components

Postby fhessel » Wed Sep 25, 2019 7:42 pm

I used the C++ STL to implement the web server, the included vector is part of it.

I'm not aware of the details of the IDF's build process, but you may just need to tell the compiler and linker that you want the C++ standard library to be included by adding -lstdc++ to the compiler and linker flags. This will also help you with the iostreams that are used in some other parts of the lib.

Other than that, removing the Arduino dependencies should be doable, as the library does not heavily rely on the Arduino framework. It's mainly included to provide a common API for Arduino developers by implementing the Print interface on the HTTPResponse class etc.

Who is online

Users browsing this forum: Bing [Bot], iParcelBox and 71 guests