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 7189739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:21:57+00:00 2026-05-28T19:21:57+00:00

(Environment: gcc 4.6, c++11, glibc 2.13, binutils 2.21) Please consider the follow linking demo

  • 0

(Environment: gcc 4.6, c++11, glibc 2.13, binutils 2.21)

Please consider the follow “linking” demo for background:

foo.h:

#pragma once
#include <iostream>

void foo();

foo1.cpp:

#include "foo.h"

void foo()
{
    std::cout << "foo1";
}

foo2.cpp:

#include "foo.h"

void foo()
{
    std::cout << "foo2";
}

main.cpp:

#include "foo.h"

int main()
{
    foo();
}

Makefile:

compile: main.o foo1.o foo2.o

case1: compile
    g++ -o case1.x main.o

case2: compile
    g++ -o case2.x main.o foo1.cpp

case3: compile
    g++ -o case3.x main.o foo1.o foo2.o

clean:
    rm *.o *.x

We have a function declaration as follows:

void foo();

and we have multiple definitions of it, one in foo1.cpp and a different one in foo2.cpp.

We compile the .cpp files into .o files:

$ make compile

No problem, main.o, foo1.o and foo2.o are produced.

Case 1

We then try and link just main.o:

$ make case1
error: undefined reference to 'foo()'

Ok, main.o references foo but no definition has been linked.

Case 2

Now we try and link main.o and foo1.o

$ make case2

There is a single definition of foo in foo1.o, so it links without error.

Case 3

Now we try and link main.o foo1.o and foo2.o

$ make case3
error: multiple definition of 'foo()'

The linker finds two definitions of foo in foo1.o and foo2.o, so rather than taking a random one it fails fast with an error and doesn’t link.

Questions

My questions are:

  1. Is it possible to mark the definition of foo1.cpp’s version of foo (possibly with some sort of gcc attribute or pragma) as “weak”? Meaning specifically that I want the behavior of case 1 and case 2 to remain the same, while the behavior of case 3 changes to successfully link without warning or error and the definition of foo in foo2.cpp is linked (overriding the “weak” definition in foo1.cpp).

  2. Is it possible to test at runtime (and assign to a boolean variable) whether the weak version of foo was linked, or the strong one (without executing the function obviously). ie implement a function in main as follows.

    template<typename FunctionPtr>
    bool is_weak_linked(FunctionPtr fp);
    

is_weak_linked(foo) should return true in case 2 and false in case 3.

  • 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-28T19:21:58+00:00Added an answer on May 28, 2026 at 7:21 pm

    To #1:

    void foo() __attribute__((weak));
    void foo()
    {
        ...
    }
    

    To #2: I don’t believe so, not with the static linking you’re doing. That information is lost at link time.

    Edit

    You can always abstract your way around #2. Some examples:

    // Foo.h
    extern void foo();
    extern "C" void default_foo();
    extern const bool is_weak_foo_linked;
    template <void (*)()> bool is_weak_linked();
    
    // Foo1.cc
    extern "C" void default_foo() {...}
    void foo() __attribute__((weak, alias("default_foo")));
    
    extern const bool __attribute__((weak)) is_weak_foo_linked=true;
    template <> bool is_weak_linked<foo>() __attribute__((weak));
    template <> bool is_weak_linked<foo>() {return true;}
    
    // Foo2.cc
    void foo() {...}
    extern const bool is_weak_foo_linked=false;
    template <> bool is_weak_linked<foo>() {return false;}
    
    // main.cc
    #include "foo.h"
    #include <iostream>
    int main() {
        foo();
        std::cout<<is_weak_foo_linked<<std::endl;
        std::cout<<is_weak_linked<foo>()<<std::endl;
        std::cout<<(foo==default_foo)<<std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Environment: gcc/g++ 4.6.1 in -std=gnu++0x mode on Linux 3.0 / x86_64...) #include <stdlib.h> #include
I have a windows build environment using cygwin and GCC, and am linking to
Is there an environment variable for GCC/G++ to look for .h files during compilation?
Environment: I am using MS-VC++ 6.0, I include a group of header file with
I'm working in a Linux environment with C++, using the GCC compiler. I'm currently
Developing environment: GNU GCC (g++) 4.1.2 While I'm trying to investigate how to increase
I've installed cygwin environment on Windows. There is gcc 4.3. How to install gcc
I'm working on Linux gcc environment and I need to initilize function arguments that
I was trying this program from Advance Programming in Unix Environment. #include<stdio.h> #include<signal.h> static
Working on a Windows 7 servicepack 1 environment with cygwin gcc compiler. The following

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.