[英]g++ using a object file in external directory
内容:
我目前正在学习在树莓派b +模型(上周开始)上进行c / c ++编译,但是在使用目标文件中的头文件时遇到了麻烦。
文件位置:
main.cpp-〜/ src / c ++ / projects / web_server
http_socket.o-〜/ src / c ++ / web
g ++的g ++输出g++ -o webserver ~/src/c++/web/http_socket.o -std=c++0x main.cpp
main.cpp:3:25:致命错误:http_socket.h:没有此类文件或目录编译终止。
main.cpp
#include "http_socket.h"
int main() { return 0; }
http_socket.cpp
#include "http_socket.h"
#include <string>
using namespace std;
http_responce::http_responce(string status_Code, string date, string server, string content_Type, string content_Length) {
Status_Code = status_Code;
Date = date;
Server = server;
Content_Type = content_Type;
Content_Length = content_Length;
}
http_socket.h
#ifndef HTTP_SOCKET_H
#define HTTP_SOCKET_H
#include <string>
using namespace std;
class http_responce
{
public:
string Status_Code;
string Date;
string Server;
string Content_Type;
string Content_Length;
http_responce(string status_Code, string date, string server, string content_Type, string content_Length);
private:
protected:
};
#endif
补充说明:
我对这种语言非常陌生,并且习惯于使用IDE,因此,如果我错过了一些琐碎的事情,请原谅我。
尝试将http_socket.h文件放在要编译的目录中。 这应该是最简单的解决方案。 基本上发生的是编译器找不到您指定的头文件。
参考:
正如您告诉g++
在哪里可以找到http_socket.o
对象文件一样,您需要帮助它并告诉它可以在哪里找到要从main.cpp
中包含的http_socket.h
标头文件。 您可以通过将-I
preprocessor选项传递给带有包含http_socket.h
的目录的g++
来http_socket.h
。
假设http_socket.h
与http_socket.h
位于同一文件夹中,则应使用http_socket.o
-I ~/src/c++/web
以便构建webserver
完整命令行如下:
g++ -I ~/src/c++/web -o webserver ~/src/c++/web/http_socket.o -std=c++0x main.cpp
从GCC 文档中 :
-I
dir
将目录dir添加到要搜索头文件的目录列表中。 在标准系统包含目录之前,将搜索
-I
命名的目录。 如果目录dir是标准的系统包含目录,那么将忽略该选项,以确保不会破坏系统目录的默认搜索顺序和系统标头的特殊处理。 如果dir以=
开头,则=
将被sysroot前缀替换; 请参见--sysroot
和-isysroot
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.