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.
File locations:
main.cpp - ~/src/c++/projects/web_server
http_socket.o - ~/src/c++/web
g++ output for 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
#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
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.
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
. You do this by passing the -I
preprocessor option to g++
with the directory that contains 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:
g++ -I ~/src/c++/web -o webserver ~/src/c++/web/http_socket.o -std=c++0x main.cpp
From the GCC documentation:
-I
dir
Add the directory dir to the list of directories to be searched for header files. Directories named by
-I
are searched before the standard system include directories. 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 . If dir begins with=
, then the=
will be replaced by the sysroot prefix; see--sysroot
and-isysroot
.