Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:13:17+00:00 2026-05-24T04:13:17+00:00

I’ve spent days in a weird problem and finally discover that there were two

  • 0

I’ve spent days in a weird problem and finally discover that there were two inline function of the same signature in the project and they caused the problem. To simplify the situation here is an example: two cpp file:

a.cpp

#include <iostream>

void b();

inline void echo()
{
    std::cout << 0 << std::endl;
}

int main()
{
    echo();
    b();
    return 0;
}

and b.cpp

#include <iostream>

inline void echo()
{
    std::cout << 1 << std::endl;
}

void b()
{
    echo();
}

Please note that inline functions echo have the same signature but different implements. Compile and run

g++ a.cpp b.cpp -o a.out && ./a.out

Or like this

g++ a.cpp -c
g++ b.cpp -c
g++ a.o b.o -o a.out
./a.out

It prints 0 0. (I was using g++ 4.6.1 for that, and I tested with clang++ 2.9, same result)

That won’t happen if turning on optimization, like

g++ -O3 a.cpp b.cpp -o a.out && ./a.out

It is 0 1 this time.

My question is, no matter the result or how the compilation performs, there is no error or even warning about I have defined inline functions multiple times. What on earth happens to the compiler and linker in this kind of situation?

EDIT:

Take a look at the symbols in the object file

nm a.o b.o | c++filt

Both files have the record echo(). So I think the problem happens at the link time. Could it be said that the linker randomly picks one implementation and discard all other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:13:18+00:00Added an answer on May 24, 2026 at 4:13 am

    The compiler is not required to diagnose this ODR violation, and it is not trivial. The inline keyword means that different translation units might have the same symbol, so it is marked weak by the compiler. The basic use case is a function defined inline in a header: all translation units that include the header will have the definition, and it is perfectly fine. The compiler only needs to discard all but one definition and use that definition everywhere.

    Detecting whether the different definitions are exact matches is a complex problem. The linker would have to analyze the generated binary implementation and determine whether the two binary codes relate to the same source code or not. Most compilers do not have support to determine this.

    As of your particular problem, I cannot possibly know the rationale that led to the two functions being marked inline, but a common error is using the inline keyword to represent optimize rather than don’t complain of repetitions at link time. The inline keyword makes sense in headers, but not so much in cpp files. In cpp files, if you want to factor some piece of code into a helper function, that function should be either marked static or be defined within an unnamed namespace. If the function is static then the compiler knows that all of the usages of that function are within your translation unit, and it has greater knowledge to decide whether it wants to inline or not the function call (note that it can inline even if you don’t tell it to, in the same way that it can decide not to inline even if you tell it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.