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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:04+00:00 2026-05-31T19:44:04+00:00

This is a question on C/C++ function definitions. The code discussed is the static

  • 0

This is a question on C/C++ function definitions. The code discussed is the static library libRmath that provides the definitions in the Rmath.h header file in R.

The documentation provided for the library states that it is optional for the user to provide the function definition for the function double unif_rand(void).

So my question is if such a function definition is optional, wouldn’t there be a problem of multiple function definitions which is not allowed in C/C++?

Edit:
It might be tempting to speculate on how things works without looking at the source code, but that is not what I want. I am interested to know how it really works, so you would probably need to read the source code and documentations to answer this question.

  • 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-31T19:44:06+00:00Added an answer on May 31, 2026 at 7:44 pm

    When you application is linked, unresolved symbols will be resolved using the libraries you provide. If you don’t define a function, it will be an unresolved symbol during linking, therefore, the linked will try to resolve that symbol using librmath, in this case. If one or more symbols can’t be resolved, you’ll get a linker error.

    However, if you do define the function in your code, it will be already defined during linking, so there will be no need to resolve it using symbols from external libraries.

    What you can’t do is to define the same symbol more than once in your application.

    Edit: Since there is a lot of debate in another answer, i’ve made a practical example. I’ve created a shared object(similar to a DLL in windows), which defines and exports a function foo:

    //lib.h
    extern "C" {
        void foo();
        void bar();
    };
    
    //lib.cpp
    #include <iostream>
    #include "lib.h"
    
    void foo() {
        std::cout << "From lib\n";
    }
    
    void bar() {
        std::cout << "Bar, calling foo\n";
        foo();
    }
    

    In order to test this shared object, i’ve created an application which is linked with it:

    //test.cpp
    #include <iostream>
    #include "lib.h"
    
    void foo() {
        std::cout << "From app\n";
    }
    
    int main() {
        bar();
    }
    

    I’ve compiled both, the shared object and the application:

    g++ lib.cpp -o libtest.so -Wall -fPIC -shared -Wl,--export-dynamic -Wl,-soname,libtest.so -Wl,-z,defs
    g++ test.cpp -o test -L. -ltest
    

    And when i execute test, setting the library path to ".", so my shared object can be loaded, i get this output:

    matias@master:/tmp$ LD_LIBRARY_PATH="." ./test
    Bar, calling foo
    From app
    

    As you can see, the foo function defined in the application(not the shared object) is called. You can basically do this for every exported symbol in a shared object.

    EDIT2: i’ve added another exported function in lib.h. The application now calls this function, which ends up calling foo. The result is the same, as expected.

    EDIT3: Ok, let’s go deeper. This is the dump from function bar:

    Dump of assembler code for function bar@plt:
       0x0804855c <+0>: jmp    DWORD PTR ds:0x804a004
       0x08048562 <+6>: push   0x8
       0x08048567 <+11>:    jmp    0x804853c
    

    If we go to address 0x804a004:

    Dump of assembler code for function _GLOBAL_OFFSET_TABLE_:
       0x08049ff4 <+0>: or     BYTE PTR [edi+0x804],bl
       0x08049ffa <+6>: add    BYTE PTR [eax],al
       0x08049ffc <+8>: add    BYTE PTR [eax],al
       0x08049ffe <+10>:    add    BYTE PTR [eax],al
       .....
    

    As you can see, it’s jumping to the Global Offset Table. You can read about the GOT here and here. Dynamic symbols(which are resolved at runtime) are stored in this table. Whenever you call a symbol that should be resolved at runtime, you actually jump to this table, and then jump to the address that is stored in the this table’s corresponding entry. Since the application defines foo, the GOT contains the address of the definition from test.cpp, not the one in our shared object.

    EDIT4: Okay, last edit. Quoting from the documentation:

    You will need to supply the uniform random number generator

     double unif_rand(void)
    

    or use the one supplied (and with a dynamic library or DLL you will
    have to use the one supplied(…)

    The documentation clearly says that you cannot provide you own implementation of unif_rand if you’re using the dynamic library. Therefore, i believe that what i pointed out, actually answers your question.

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

Sidebar

Related Questions

This question is based on this thread . The code function man() { man
In the answers to this question , we read that function f() {} defines
This is sample of function in the Staff controller for this question function newStaff()
This question is regarding getopt function in php. I need to pass two parameter
This question discusses encrypting data on the iPhone using the crypt() function. As an
This question is simple. What function would I use in a PHP script to
This question is based on the thread . I have the shell function function
Following this question , I use this function to convert arrays to objects, function
This is something of an extension to this question: Dispatching to correct function with
I have this Python code that I found online and would like to know

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.