CMake not linking static libraries properly when using LLVM on Windows

Viewed 325

I am using Windows 10 and CMake to build my project (using MinGW Makefiles as the generator), while choosing Clang/Clang++ (LLVM) as my compiler due to my Language Server Protocol also being for Clang.

However, when trying to link a static library I had created to my main executable, none of the definitions in the translation units were able to be resolved.

For a simple recreation:

File system
+ root/
    + main.cpp
    + foobar.cpp
    + foobar.hpp
    + CMakeLists.txt
// main.cpp
#include <iostream>
#include "foobar.hpp"

int main( int argc, char** argv ) {
    foo();

    std::cin.get();
    return 0;
}
// foobar.cpp
#include "foobar.hpp"

void foo() {
    std::cout << "bar" << std::endl;
}
// foobar.hpp
#pragma once
#ifndef FOOBAR_HEADER_H
#define FOOBAR_HEADER_H

#include <iostream>

void foo();

#endif
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)

project(linker_fail
    VERSION 1.0.0
    LANGUAGES C CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_library(foobar
    foobar.cpp
    foobar.hpp)

add_executable(${PROJECT_NAME}
    main.cpp)

target_link_libraries(${PROJECT_NAME} PUBLIC
    foobar)

Then for building:

root> cmake -S . -B bin -G "MinGW Makefiles"
root> cmake --build bin

lld-link: error: undefined symbol: void __cdecl foo(void)

Does anyone know how to fix this? Should I have passed some arguments to the compiler/linker? Or does Clang just not allow static linking?

0 Answers
Related