0

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.

Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
Garry Mason
  • 33
  • 1
  • 8

2 Answers2

0

Try placing the http_socket.h file in the directory you are trying to compile. 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"?

Community
  • 1
  • 1
NevDev
  • 604
  • 5
  • 8
  • Thats is indeed a quick fix, but I prefer to keep the files in their specific directory to avoid having many dupes. – Garry Mason Jul 24 '15 at 14:35
0

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.

Community
  • 1
  • 1
Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
  • Ahhh I see, so it's needs to see the header file, I assumed that the object file I made contained it. I've been going through TheNewBoston's c++ tutorial on youtube. I remember him saying that the cpp file is compiled in machine code and the header file left as plain text for users to view, though somehow that didn't come to mind this time. My bad. – Garry Mason Jul 24 '15 at 14:31