Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake doesn't link C and C++ static libraries (undefined reference to function)

Tags:

c++

c

cmake

I tried to reproduce minimal problem. When I run CMake+Make on Ubuntu I get error

funccpp.cpp:(.text+0x5): undefined reference to `FuncC'

i.e. exported function in C library is not found when importing in C++ library. When I try to manually compile using g++ main.cpp funcc.c funccpp.cpp it successfully compiles final program. How to fix CMake issue?

For reference, when I run nm libfuncc_lib.a I get line T FuncC (so symbol is external and defined in Text section), when I run nm libfunccpp_lib.a I get U FuncC (so symbol is Undefined and should be linked from outside).

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(Test C CXX)

set (SRC_CPP funccpp.cpp)
set (SRC_C funcc.c)
set (SRC_MAIN main.cpp)

add_library(funcc_lib STATIC ${SRC_C})
add_library(funccpp_lib STATIC ${SRC_CPP})
add_executable(prog ${SRC_MAIN})
target_link_libraries(prog funcc_lib funccpp_lib)

main.cpp

#include "funccpp.h"

int main() {
    FuncCPP();
    return 0;
}

funccpp.h

#ifndef FUNCCPP_H
#define FUNCCPP_H

void FuncCPP();

#endif

funccpp.cpp

#include "funcc.h"

void FuncCPP() {
    FuncC();
}

funcc.h

#ifndef FUNCC_H
#define FUNCC_H

#ifdef __cplusplus
extern "C" {
#endif

void FuncC();

#ifdef __cplusplus
}
#endif

#endif // FUNCC_H

funcc.c

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

void FuncC() {
    printf("Hello, World!\n");
}
like image 761
Arty Avatar asked Aug 07 '18 11:08

Arty


1 Answers

The problem here is, that the linker relies on the order of the libraries. With

target_link_libraries(prog funcc_lib funccpp_lib)

It first links funcc_lib and then funccpp_lib. But it never gets back to funcc_lib. Since funccpp_lib depends on funcc_lib, you have to change the order of the libraries:

target_link_libraries(prog funccpp_lib funcc_lib)

For additional information, see this discussion.

like image 173
Stanley F. Avatar answered Nov 03 '22 02:11

Stanley F.