Search code examples
c++visual-c++linker

C++: ODR and extern "C" on MSVC linker


I'm testing out how the Windows link.exe program reacts when presented with ODR violations. My intention is that the linker will fail when it notices that my program has two definitions of the same function (dynamic_func), one of which comes from a DLL. I have the following:

/* main.cpp */
#include "dynamic_lib.h"
#include "static_lib.h"

int main()
{
    dynamic_func();
    static_func();

    return 0;
}
/* dynamic_lib.h */
#ifndef DYNAMIC_LIB_H
#define DYNAMIC_LIB_H

extern "C" {

#ifdef HEADER_ONLY
#   define INLINE inline
#   define DYNAMIC_LIB_API
#else
#   define INLINE
#   ifdef _WIN32
#       ifdef BUILDING_LIB
#           define DYNAMIC_LIB_API __declspec(dllexport)
#       else
#           define DYNAMIC_LIB_API __declspec(dllimport)
#       endif /* BUILDING_LIB */
#   else
#       define DYNAMIC_LIB_API
#   endif /* _WIN32 */
#endif /* HEADER_ONLY */

DYNAMIC_LIB_API void dynamic_func();

}

#ifdef HEADER_ONLY
#   include "dynamic_lib.cpp"
#endif

#endif /* DYNAMIC_LIB_H */
/* dynamic_lib.cpp */
#include "dynamic_lib.h"

INLINE void dynamic_func()
{}
/* static_lib.h */
#ifndef STATIC_LIB_H
#define STATIC_LIB_H

extern "C" {

void static_func();

}

#endif /* STATIC_LIB_H */
/* static_lib.cpp */
#include "static_lib.h"
#include "dynamic_lib.h"

void static_func()
{
    dynamic_func();
}
/* CMakeLists.txt */
cmake_minimum_required(VERSION 3.20.0 FATAL_ERROR)

project(Test CXX)

set(CMAKE_CXX_STANDARD 20)

add_library(dynamic_lib SHARED
    dynamic_lib.cpp
)

target_compile_definitions(dynamic_lib PRIVATE BUILDING_LIB)

add_library(static_lib STATIC
    static_lib.cpp
)

target_compile_definitions(static_lib PRIVATE HEADER_ONLY)

add_executable(main
    main.cpp
)

target_link_libraries(main PUBLIC
    dynamic_lib
    static_lib
)

When trying to link main.exe, this correctly fails with a LNK2005 symbol redefinition error for dynamic_func.

However, if I change the example so that static_lib.cpp itself defines dynamic_func, the linking step works fine:

/* static_lib.cpp */
#include "static_lib.h"

void dynamic_func()
{}

void static_func()
{
    dynamic_func();
}

In both cases the compiler and linker invocations are identical. Furthermore, the first example seems to build fine on both gcc and clang: https://godbolt.org/z/Phc47Wcaa

I understand both examples should be equivalent since static_lib uses dynamic_lib in header-only mode, and therefore the definition of dynamic_func ends up appearing in the same translation unit. The usage of inline doesn't seem to change anything. However, analyzing the preprocessed output of static_lib.cpp I saw that what makes the first example fail is the declaration of dynamic_func within extern "C"; adding this to the second example makes it fail in the same way as the first.

This is surprising since I don't know why extern "C" would play any role here. Am I misunderstanding something?


Solution

  • As indicated by @rustyx in one of the comments, it seems like extern "C" does affect the symbols' names even though they're in the global namespace. Removing extern "C" from all the declarations also triggers the linker error. As @rustyx said, mixing extern "C" and no-extern "C" declarations creates duplicate functions that can coexist.