[英]g++ using a object file in external directory
Context: 内容:
I am current learning c/c++ compiling on my raspberry pi b+ model (started last week), but having trouble using a header file inside a object file. 我目前正在学习在树莓派b +模型(上周开始)上进行c / c ++编译,但是在使用目标文件中的头文件时遇到了麻烦。
File locations: 文件位置:
main.cpp - ~/src/c++/projects/web_server main.cpp-〜/ src / c ++ / projects / web_server
http_socket.o - ~/src/c++/web http_socket.o-〜/ src / c ++ / web
g++ output for g++ -o webserver ~/src/c++/web/http_socket.o -std=c++0x main.cpp
g ++的g ++输出
g++ -o webserver ~/src/c++/web/http_socket.o -std=c++0x main.cpp
main.cpp:3:25: fatal error: http_socket.h: No such file or directory compilation terminated. main.cpp:3:25:致命错误:http_socket.h:没有此类文件或目录编译终止。
main.cpp main.cpp
#include "http_socket.h"
int main() { return 0; }
http_socket.cpp 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 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
Addition note: 补充说明:
I am very new to this language and accustomed to using an IDE, so please forgive me if it is something very trivial that I have missed. 我对这种语言非常陌生,并且习惯于使用IDE,因此,如果我错过了一些琐碎的事情,请原谅我。
Try placing the http_socket.h file in the directory you are trying to compile. 尝试将http_socket.h文件放在要编译的目录中。 This should be the easiest solution.
这应该是最简单的解决方案。 Basically what's happening is that the compiler can't find the header file you specified.
基本上发生的是编译器找不到您指定的头文件。
Reference : 参考:
What is the difference between #include <filename> and #include "filename"? #include <文件名>和#include“文件名”有什么区别?
Just as you are telling g++
where to find the http_socket.o
object file, you need to help it out and tell it where it can find the http_socket.h
header file you are including from main.cpp
. 正如您告诉
g++
在哪里可以找到http_socket.o
对象文件一样,您需要帮助它并告诉它可以在哪里找到要从main.cpp
中包含的http_socket.h
标头文件。 You do this by passing the -I
preprocessor option to g++
with the directory that contains http_socket.h
. 您可以通过将
-I
preprocessor选项传递给带有包含http_socket.h
的目录的g++
来http_socket.h
。
Assuming http_socket.h
is in the same folder as http_socket.o
, you would use -I ~/src/c++/web
so that your full command line for building webserver
would be the following: 假设
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
From the GCC documentation : 从GCC 文档中 :
-I
dir
-I
dir
Add the directory dir to the list of directories to be searched for header files.
将目录dir添加到要搜索头文件的目录列表中。 Directories named by
-I
are searched before the standard system include directories.在标准系统包含目录之前,将搜索
-I
命名的目录。 If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated .如果目录dir是标准的系统包含目录,那么将忽略该选项,以确保不会破坏系统目录的默认搜索顺序和系统标头的特殊处理。 If dir begins with
=
, then the=
will be replaced by the sysroot prefix;如果dir以
=
开头,则=
将被sysroot前缀替换; see--sysroot
and-isysroot
.请参见
--sysroot
和-isysroot
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.