Ubuntu - 没有这样的文件或目录 #include <string>

Ubuntu - No such file or directory #include <string>

本文关键字:#include gt lt string 文件 Ubuntu      更新时间:2023-10-16

我正在尝试编写这样的代码:(未完成)

 #include"header.h"
 #include<stdlib>
 #include<stdio.h>
 #include<libpq-fe.h>
 #include<string>
 int main(){
  std::string pguser = "XYZ";
  std::string pgpassword = "XYZ";
  std::string pghost="XYZ";
  std::string pgdbname = "XYZ";
  int i;
  PGconn *conn;
  PGresult *res;
  connstr = "user=";
  connstr += pguser;
  connstr +="password=";
  connstr += pgpassword;
  connstr +="host=";
  connstr += pghost;
  connstr +="dbname=";
  connstr += pgdbname;   
  //DB-Login
  conn = db_login(const string &user, const string &password, const string        &host, const string &dbname);
  if(conn == true){
  printf("LOGIN SUCCESFULLY");
  }else{
     printf("LOGIN FAILED");
  }
  return 0;
  }

我的标题

 #ifndef DB_H
 #define DB_H
 #include <string>
 using namespace std;
 int db_login(const string &user, const string &password, const string  &host, const string &dbname);
 #endif

我只是想测试它(sudo gcc main.c header.h -o测试)我得到的结果是:没有这样的文件或目录#include string

我搜索了一个解决方案并尝试了解决方案,但是我遇到了相同的错误。(也安装了 -> sudo apt-get安装构建本)

请帮助

您正在编写C 代码,但试图将其编译为C -gcc是C语言前端,因此请改用g++。还将您的源文件从.c重命名为.cpp,以从make(1)获得自动支持。

#include<string>是C 标头。您在.c文件中使用C 代码,这是不正确的。可能您应该将main.c重命名为main.cpp并使用G 而不是GCC

对于那些遇到这个问题并使用make而不是明确调用gcc的人:

make VERBOSE=1

将指示其GCC还是G