0

Inkey.cpp and Key.cpp are in the same directory, /root/src.

Compile.bat is in /root.

When I run Compile.bat, I am given the message "Key.cpp: No such file or directory".

Thanks in advance for any advice.

Inkey.cpp

#include <iostream>
#include <Key.cpp>

using namespace std;

int main(int argc, char *argv[]) {
    cout << "Hello from InKey" << endl;

    Key key1;

    return 0;
}

Key.cpp

#include <iostream>

using namespace std;

class Key {
    public:
        Key() {
            cout << "New Key instantiated." << endl;
        };
};

Compile.bat

@ECHO OFF

g++ "src/Inkey.cpp" -o "out/InKey.exe"

"out\Inkey.exe"
0

2 Answers 2

1

Look up the difference between #include "filename" and #include <filename>. The former starts at the current directory, the latter uses a search path (or set of such paths). (Note that #include "filename" will fall back on the search strategy of #include <filename> if no file is found starting from the current directory).

Also, you do not usually include .cpp files, you pass them as separate arguments to the compiler and then combine them using the linker.

Sign up to request clarification or add additional context in comments.

Comments

0

First off, rename Key.cpp to Key.h. Put the declaration in Key.h. Then put the definition in Key.cpp (Don't forget to include Key.h here). Then include your Key.h in all files using Key objects.
Now, compile them with g++ -o filename.out file1.cpp file2.cpp -O3 -Wall
Also, learn to use boilerplate code

P.S. as @SoronelHaetir has suggested, use "" to include custom header files

1 Comment

I appreciate the detailed explanation. In my ignorance, I thought I could pick up CPP during a Pet Project. Compiled languages seem a bit more involved than "interpreted" ones. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.