[英]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.