How to avoid multiple definition linking error?

Active3 hr before
Viewed126 times

8 Answers


As you're using g++, you should consider putting your library functions into a namespace (they're designed to help you avoid this kind of nameing conflict). This would allow both your code and the linker to tell the difference between the methods.,Beside moving the hello() function into another source (.cpp) file or renaming the function. Is there any other methods to avoid the linking error?,The linking error specifically refers to hello. This shows up because both libraries provide definitions of "hello". There is no other linking error here. ,Since you appear to own both libraries, it's unclear why you can't rename the function...

In your main, you have this line:

load more v

The definition of yyFlexLexer::yywrap() appears in both the header and implementation file. Prefixing with inline solves the problem, but maybe a better solution would be to only output to the implementation file?,Just to clarify the issue is with the header generated by flex, as shown in this example program:,Two solutions are to #include <FlexLexer.h> instead or the fix to src/main.c above. I still believe the code is broken however, as it generates a header which cannot safely be referenced.,@lexxmark maybe you would be interested in applying the same fix to win_flex?

 		if (!do_yywrap) {
 		   outn("\nint yyFlexLexer::yywrap() { return 1; }");
load more v

General C++ Programming,UNIX/Linux Programming

load more v

#ifndef _INC_RMW_H
#define _INC_RMW_H
load more v

Some of these changes are user visible and can cause grief when porting to GCC 10. This document is an effort to identify common issues and provide solutions. Let us know if you have suggestions for improvements! , If short-term fixing of those issues is not feasible, the compiler flag -fallow-argument-mismatch (implied by -std=legacy) downgrades the error to a warning. , The GCC 10 release series differs from previous GCC releases in a number of ways. Some of these are a result of bug fixing, and some old behaviors have been intentionally changed to support new standards, or relaxed in standards-conforming ways to facilitate compilation or run-time performance. , An invalid but comparably common use is to pass scalar to such procedures:

A common mistake in C is omitting extern when declaring a global variable in a header file. If the header is included by several files it results in multiple definitions of the same variable. In previous GCC versions this error is ignored. GCC 10 defaults to -fno-common, which means a linker error will now be reported. To fix this, use extern in header files when declaring global variables, and ensure each global is defined in exactly one C file. If tentative definitions of particular variables need to be placed in a common block, __attribute__((__common__)) can be used to force that behavior even in code compiled without -fcommon. As a workaround, legacy C code where all tentative definitions should be placed into a common block can be compiled with -fcommon.

int x; // tentative definition - avoid in header files

extern int y; // correct declaration in a header file
load more v

Thus, every source file with global include should have an access to this object.,I would love to but it’s for a company so I can’t share it at the moment But to the original question there should be no problem defining a constant with the preprocessor inside a standard include guard as per C++ standards?,Include guard and simple constant defines are a standard approach, so they shouldn’t cause any linker problem. Probably global variables "//some vars" in header file might be the main culprit.,Then define this object once in any source file:

I’m finishing up porting my project from Arduino to PlatformIO. I’m getting linker errors about certain functions having multiple definitions. It reminds me of what happens when you don’t use an include guard in your header files, except I’ve double checked and I’m using them in every header file. I am however defining several preprocessor constants inside the include guards and I’m wondering if that’s allowed. Can you tell me if this is ok?

#ifndef whatever_h
#define whatever_h

//some vars
//some funcs
#define const1
#define const2

load more v

The reason for this is that since classes are types, their definitions are necessary to be able to share data between translation units. Originally, classes (structs in C) did not have any data requiring linker support; C++ introduces virtual member functions and virtual inheritance, which require linker support for the vtable, but this is usually worked around by attaching the vtable to (the definition of) a member function.,Second, I know from previous research that it is bad practice to define variables and functions in a header file, because during the linking phase you might have multiple definitions for the same name which will throw an error.,A class definition is just a kind of a blueprint for the objects of that class. It's been the same with struct since the C days. No classes or structures actually exists in the code as such.,So while (non-inline) functions may be defined at most once in the whole program (and exactly once if they are called or otherwise odr-used), classes may be defined as many times as you have translation units (source files), but no more than once per translation unit.

However, how come this doesn't happen for classes? According to another SO answer ( What is the difference between a definition and a declaration? ), the following would be a class DEFINITION:

    class MyClass {
load more v

The first example isn't inline and so if this header is included in multiple translation units then you will have multiple definitions and linker errors.,Also, headers should really always be guarded to prevent multiple definition errors in the same translation unit. That should convert your header to:,As StackedCrooked rightly mentions, including the static function definition but not using it will result in an appropriate compiler warning.,What you need in your main.c in order to use the test() function is a simple declaration, not its definition. This is achieved by including a test.h header file which contains something like:

I'm a C newbie and I was just trying to write a console application with Code::Blocks. Here's the (simplified) code: main.c:

#include <stdio.h>
#include <stdlib.h>
#include "test.c" // include not necessary for error in Code::Blocks

int main()
    //t = test(); // calling of method also not necessary
    return 0;


void test() {}

When I try to build this program, it gives the following errors:

* path * test.c | 1 | multiple definition of `_ test'|
objDebugmain.o:*path*test.c|1|first defined here|
load more v

Other "error-avoid" queries related to "How to avoid multiple definition linking error?"