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);
7
  • 5
    what do you mean when you say "I can't use a function" ? How did you compile the code? Commented Nov 15, 2021 at 12:28
  • 6
    you dont "include cpp files". You compile and link them Commented Nov 15, 2021 at 12:31
  • I code in VS Code and there is a "run" button. console says: g++ Main.cpp -o Main Commented Nov 15, 2021 at 12:31
  • It's not building and linking Header.cpp for you. Some workarounds are listed here: stackoverflow.com/questions/51720769/…, maybe that will help? Commented Nov 15, 2021 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 Commented Nov 15, 2021 at 12:37

2 Answers 2

2

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 .

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

6 Comments

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
I can mark only after few minutes. I will mark your answer as correct! Thank you
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.
@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.
@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
|
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 :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.