Ask ChatGPT to fix a C++ linker error with the complete file layout

For C++ help, give ChatGPT the language standard, compiler, complete build command and the files that command compiles. Distinguish a compilation diagnostic from a linker diagnostic. Code that compiles in one source file can fail when the same header is included by two separately compiled files.

If the linker reports multiple definitions, increasing an include guard will not necessarily help. A guard prevents repeated inclusion within one translation unit. The same guarded header can still contribute a function definition to several translation units.

Keep the two-file reproduction

This authored example deliberately defines an ordinary externally linked function in a header. Save these three files together.

format.h:

#ifndef FORMAT_H
#define FORMAT_H
int review_limit() { return 24; }
#endif

summary.cpp:

#include "format.h"
int summary_limit() { return review_limit(); }

main.cpp:

#include <iostream>
#include "format.h"
int summary_limit();

int main() {
    std::cout << review_limit() << ' ' << summary_limit() << '\n';
}

With an installed GCC-compatible C++ toolchain, a reproduction command is g++ -std=c++17 -Wall -Wextra main.cpp summary.cpp -o limits. On Windows with the Visual C++ developer environment configured, use cl /std:c++17 /EHsc main.cpp summary.cpp. Use your project's existing build command when it manages additional options.

Both source files include format.h. After preprocessing, each translation unit contains a definition of review_limit. The expected failure is a duplicate-definition linker error; its exact wording depends on the toolchain. Microsoft's program and linkage guide, checked October 24, 2026, explains translation units and the one definition rule.

Put one definition in one source file

For an ordinary shared implementation, change the header to a declaration:

#ifndef FORMAT_H
#define FORMAT_H
int review_limit();
#endif

Add format.cpp:

#include "format.h"
int review_limit() { return 24; }

Compile all three source files. For GCC-compatible tools, the command becomes g++ -std=c++17 -Wall -Wextra main.cpp summary.cpp format.cpp -o limits. The expected program output is 24 24.

Leaving format.cpp out of the build creates a different failure: callers have a declaration but no linked definition. That is why the complete build command belongs in the question. A correct source edit alone cannot make an omitted file participate in the build.

Decide whether this should be header-defined

For an intentionally header-defined function, inline int review_limit() { return 24; } is another design. An inline function may have permitted identical definitions in multiple translation units under the language's rules. inline does not promise that the compiler will substitute the body at every call.

Do not add static merely to silence the linker. It gives each translation unit its own internally linked function, which can change identity and, for other implementations, associated state. Choose between a shared source definition and a valid header implementation based on the library's design.

Give ChatGPT the build evidence

Explain this C++ build failure before proposing a patch.
Compiler/version, language standard, OS: [ENTER]
Exact build command and complete diagnostic: [PASTE]
File tree and complete relevant headers/source files: [PASTE]

Identify whether the failure happens during compilation or linking.
Trace which translation units contain each definition. Explain why
the include guard does or does not solve this failure.
Show a declaration-in-header/one-definition-in-source repair and
the updated build inputs. Explain an inline alternative separately.
Do not change linkage with static by convention or suppress errors.
Separate expected results from commands actually executed.

If the problem is missing evidence across several files, the debugging guide helps reduce it to a reproducible case. Keep the final file layout and build command with the repair so the next developer can reproduce the same linked program.

Keep reading