Hey,

I have seen some code examples were people include there own files, e.g

#include <myFile.h>

do i just have to save a file called myFile.cpp?

and if so can i declare a function in myFile and then use it in untitled1 without declaring it just including myFile. Thanks.

Dani AI

Generated

raised the right basic question and gave a solid starter answer. A compact clarification that fills the usual gap: #include is a preprocessor text substitution — the header’s text is inserted into the .cpp before compilation. That supplies declarations so the compiler knows how to type-check calls, but the actual function body must exist in some compiled object file for the linker to resolve. If a body is missing the linker reports an "undefined reference"; if the same non-inline body appears in multiple object files the linker reports "multiple definition."

Practical rules and exceptions: keep declarations (prototypes, class declarations, extern globals) in headers and put implementations in a single .cpp. The main exceptions are templates, inline functions/variables, constexpr, or deliberate header-only libraries — those are defined in headers on purpose. Avoid using namespace in headers and prefer forward declarations when only pointers/references are needed to reduce compile-time coupling.

Quick build and troubleshooting checklist:

g++ untitled1.cpp myFile.cpp -o untitled1
  • "undefined reference": confirm the .cpp with the implementation is compiled and linked; check exact signatures and extern "C" if mixing C/C++.
  • "multiple definition": move the body to one .cpp or make it inline/static if the duplicated definition is intended.
  • Prevent accidental double-inclusion with include guards or #pragma once. Keep headers minimal (no heavy includes) to speed builds.

In short: declarations in headers, definitions in one implementation file, and awareness of compiler vs. linker responsibilities will avoid the common errors that follow from copying bodies into headers. 's include-guard advice is part of that complete picture.

You would actually name the file myFile.h, not myFile.cpp. When you #include something you are basically taking the contents of the header (*.h) file and dumping them into your current .cpp file. If the file you need to include is in your current working directory (or if you're specifying an absoltue path), use "". If the file is in one of your compiler's include directories, use <>.

It is a bad idea to define functions in a header file. If you have this in myFile.h:

int MyFunc()
{
    return 5;
}

and you have two .cpp files in your project, and they both #include myFile.h, you'll get an error, because you're trying to re-define a function, a no-no. Instead, have prototypes in your header files. Like:
myfile.h

int MyFunc();

and have definitions in a .cpp file:
myfile.cpp

#include "myfile.h"

int MyFunc()
{
    return 5;
}

Then any amount of other files can #include myfile.h with no problem, as long as myfile.cpp is also in the project.

One last thing, it's good to put the code in your header files in #ifndef blocks. Like this:

#ifndef MY_FILE_H
#define MY_FILE_H

int MyFunc();

#endif

This way, if you end up #including the same file twice in one .cpp file (which can happen when you have lots of #includes), you won't have an issue, since the code is only executed once.

commented: Nice post, easy to understand :P +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.