0

I just started learning C++ and I have been doing some practice on using user-defined header files. I just made a simple code on doing math operations. Many videos online use #include "filename.h in their main program without even "connecting" the functions file with its header file. For instance, in my practice, I made two files, one called main.cpp and the other called math.cpp which has a header file called math.h. All these files are under one folder.

math.cpp

int add (int x, int y){
    return x + y;
}

int sub(int x, int y){
    return x - y ;
}

int multiply(int x, int y){
    return x * y;
}

math.h

#pragma once

int add (int x, int y);

int sub(int x, int y);

int multiply(int x, int y);

main.cpp

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

using namespace std;

int main (){
    cout << add(1,2);
    return 0;
}

Interestingly, if I write #include "math.cpp" in the math.h file, it works without any errors but I have read countless times that this should not be done. Additionally, many tutorials do not even "connect" the header and the cpp file and just include the header in the main file. When I try to do this, it doesn't work. The only time it works is when I change the math.h code to

#pragma once
#include "math.cpp"

int add (int x, int y);

int sub(int x, int y);

int multiply(int x, int y);

I am still not entirely sure what the syntax should be when using user-defined header files. How does the header file we create know which cpp file to look at? Does it have anything to do with the fact I am using macOS that none of the tutorials seem to work?

tadman
  • 208,517
  • 23
  • 234
  • 262
riyam
  • 1
  • 2
    You should probably look at [how c++ code is compiled](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work). The main idea regarding your question is the division into compiling and linking – IWonderWhatThisAPIDoes Apr 22 '21 at 05:56
  • 1
    The whole point of separating source files is so they can be independently compiled as separate translation units. When you `#include`, that adds the file's contents as if it's code in whatever file you're compiling. The point of headers is to specify the stuff that users of a library need to know (its data types, functions etc) without caring about the underlying implementation. You are probably getting errors because you did not add all your compiled objects to the linker. If you compile `main.cpp` and `math.cpp`, you then need to link the outputs from those two to produce an executable. – paddy Apr 22 '21 at 05:59
  • Thank you! If I am doing a more complex project with multiple user-defined header files, would compiling all of them be smart? There must be another way? – riyam Apr 22 '21 at 06:25
  • _"Does it have anything to do with the fact I am using macOS that none of the tutorials seem to work?"_ No, it has to do with the fact that almost all tutorials on YouTube have a very low quality. The best way to learn such a complex programming language is by reading a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Good (and even most bad) introduction books describe the C++ build process and how projects with multiple files are handled. –  Apr 22 '21 at 07:54
  • _"If I am doing a more complex project with multiple user-defined header files, would compiling all of them be smart? There must be another way?"_ Usually you use an IDE or a separate build system. An IDE usually automatically adds all source files in your project to the build and compiles each translation unit. Then it links all object files to an executable. In a build system you have to configure your project. IMHO the best way to learn this is by compiling your project from terminal and then learning about Make. After you understood the build process you should be able to configure an IDE. –  Apr 22 '21 at 08:02
  • "Thank you! If I am doing a more complex project with multiple user-defined header files, would compiling all of them be smart? " - 1. I don't know why you call them "user defined header files". They are simply header files. 2. Use any sort of project file that generates a Makefile, like qmake or cmake project files. Even if you let your IDE write those as the other guy suggested, make sure you understand how they work. Also make sure to have a basic understanding how a Makefile works. – Aziuth Apr 22 '21 at 08:03

0 Answers0