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

  • SEARCH
  • Home
  • 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 875779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:19:43+00:00 2026-05-15T11:19:43+00:00

Recently I’ve come across a problem in my project. I normally compile it in

  • 0

Recently I’ve come across a problem in my project. I normally compile it in gcc-4, but after trying to compile in gcc-3, I noticed a different treatment of inline functions. To illustrate this I’ve created a simple example:

main.c:

#include "header.h"
#include <stdio.h>

int main()
{
    printf("f() %i\n", f());
    return 0;
}

file.c:

#include "header.h"
int some_function()
{
    return f();
}

header.h

inline int f()
{
    return 2;
}

When I compile the code in gcc-3.4.6 with:

gcc main.c file.c -std=c99 -O2

I get linker error (multiple definition of f), the same if I remove the -O2 flag. I know the compiler does not have to inline anything if it doesn’t want to, so I assumed it placed f in the object file instead of inlining it in case of both main.c and file.c, thus multiple definition error. Obviously I could fix this by making f static, then, in the worst case, having a few f‘s in the binary.

But I tried compiling this code in gcc-4.3.5 with:

gcc main.c file.c -std=c99 -O2

And everything worked fine, so I assumed the newer gcc inlined f in both cases and there was no function f in the binary at all (checked in gdb and I was right).

However, when I removed the -O2 flag, I got two undefined references to int f().
And here, I really don’t understand what is happening. It seems like gcc assumed f would be inlined, so it didn’t add it to the object file, but later (because there was no -O2) it decided to generate calls to these functions instead of inlining and that’s where the linker error came from.

Now comes the question: how should I define and declare simple and small functions, which I want inline, so that they can be used throughout the project without the fear of problems in various compilers? And is making all of them static the right thing to do? Or maybe gcc-4 is broken and I should never have multiple definitions of inline functions in a few translation units unless they’re static?

  • 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-15T11:19:44+00:00Added an answer on May 15, 2026 at 11:19 am

    Yes, the behavior has been changed from gcc-4.3 onwards. The gcc inline doc (http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Inline.html) details this.

    Short story: plain inline only serves to tell gcc (in the old version anyway) to
    inline calls to the from the same file scope. However, it does not tell gcc that
    all callers would be from the file scope, thus gcc also keeps a linkable version
    of f() around: which explains your duplicate symbols error above.

    Gcc 4.3 changed this behavior to be compatible with c99.

    And, to answer your specific question:

    Now comes the question: how should I define and declare simple and small functions, which I want inline, so that they can be used throughout the project without the fear of problems in various compilers? And is making all of them static the right thing to do? Or maybe gcc-4 is broken and I should never have multiple definitions of inline functions in a few translation units unless they’re static?

    If you want portability across gcc versions use static inline.

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

Sidebar

Related Questions

Recently I noticed (after others have extended the project) that the compile time significantly
Recently i've switched to PHP 5.3+ and after that migration i learned that the
Recently I've started hearing about POJOs (Plain Old Java Objects). I googled it, but
Recently I've started to develop Android Soft Keyboard and got some problem with preferences.
Recently i saw this piece of code. Shouldnt this line be a compile error?
Recently we added a audit_logs table to the database, and after some frustration I
Recently i was studying the Passing by Reference, I come to know following ways
Recently, I came across this one stone kills multiple birds framework - http://phonegap.com/ .
Recently my Project manager asked to write comments, summary and #regions for all the
Recently Mercurial has added certificate validation when connecting to HTTPS servers. I'm trying to

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.