0

I've watched several tutorials on C++ header files and did EXACTLY what they were showing, but I can't really understand why I can't use a function from other .cpp file.

Main.cpp

#include <iostream>
#include "Header.h"

int main() {
    std::cout << sum(2, 2);

    return 0;
}

Header.cpp

#include "Header.h"

int sum(int a, int b) {
    return (a + b);
}

Header.h

#pragma once

int sum(int a, int b);
Chief
  • 3
  • 5
  • 5
    what do you mean when you say "I can't use a function" ? How did you compile the code? – 463035818_is_not_an_ai Nov 15 '21 at 12:28
  • 6
    you dont "include cpp files". You compile and link them – 463035818_is_not_an_ai Nov 15 '21 at 12:31
  • I code in VS Code and there is a "run" button. console says: g++ Main.cpp -o Main – Chief Nov 15 '21 at 12:31
  • It's not building and linking `Header.cpp` for you. Some workarounds are listed here: https://stackoverflow.com/questions/51720769/how-to-use-visual-studio-code-to-compile-multi-cpp-file, maybe that will help? – orhtej2 Nov 15 '21 at 12:35
  • VSCode by default builds only the active file into your executable. The VSCode documentation tells you how to change your `tasks.json` to build all files in the folder instead: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Nov 15 '21 at 12:37
  • Thank you to all of you but I already got an answer and realised how dumb I was – Chief Nov 15 '21 at 12:40
  • It was not dumb of you at all. Although this behavior is documented in the VSCode documentation (for all supported compilers and OSs) it is an unexpected behavior. I believe the reason for the behavior is it makes testing of small single source file programs easy (you can put them all in the same folder instead of making a separate workspace for each program) at the expense of making it more difficult once your code becomes more complex. – drescherjm Nov 15 '21 at 12:42

2 Answers2

1

Your program is working as can be seen here.

To get your program working on your machine follow these steps(assuming you're using g++ and Ubuntu:

Step 1: Create a binary/executable using the command:

g++ main.cpp Header.cpp -o myexecutable

Step 2: Test/Run your executable created in the last step using the command:

./myexecutable

Alternate Solution: A shortcut

Now if you're wondering that you've to type the name of every source file to make the executable, then you can take a sigh of relief because there is a shortcut as given below:

Assuming you have many source files(.cpp files) in your current directory and you want to compile them all without writing the names of all of them, then you can use the command:

g++ ./*.cpp -o myexecutable

The above command will create a binary/executable named myexecutable .

Jason
  • 36,170
  • 5
  • 26
  • 60
  • oh... So I have to manually add every .cpp that I use when compiling? Thank you! I thought that "run" button in VS Code will automatically link them – Chief Nov 15 '21 at 12:35
  • I can mark only after few minutes. I will mark your answer as correct! Thank you – Chief Nov 15 '21 at 12:37
  • 2
    ***So I have to manually add every .cpp that I use when compiling?*** You don't have to manually build. There are several ways you can use to get VSCode build all of your files instead of just the active file. – drescherjm Nov 15 '21 at 12:41
  • @Chief There are other ways like using a **build system** (CMake for example) which i personally use that makes building your project a lot easier. And another way would to configure VS code such that it will build all source files instead of just one source file. – Jason Nov 15 '21 at 12:44
  • @AnoopRana I know about CMake and wanted to make a Makefile but I thought: "Is there any other ways to make this work?" and came here. Now I'll use CMake everytime and everywhere – Chief Nov 15 '21 at 12:50
  • @Chief You can actually use a shortcut as i have added at the end of my answer. Check it out. Using this alternate method, you don't have to manually write the names of each source file. – Jason Nov 15 '21 at 12:56
0

By including files, the preprocessor simply makes the included file part of the file which includes the other file.

In C++, there is the one definition rule. If you include a definition multiple times in other files, you brake that rule. Simply that.

In general, the preprocessor nor the compiler has an idea what a '.h' or a '.cpp' file is, that is only a convention. But you must not have the same definition multiple times in your source files, independent if you link multiple objects or have the definition multiple times in one source file.

If you follow the convention, that you name your definition files with '.cpp', it makes no sense to include them in other files as this will end up in multiple definitions. Once in the object file generated from the '.cpp' file and once in the file which include the '*.cpp' file.

You can include the '.cpp' file once in another object file and use it, if you don't link the object file, which will be generated from that '.cpp' file. But is is a strongly advice to not do such strange things :-)

Klaus
  • 24,205
  • 7
  • 58
  • 113