0

I am a bit of a novice with cmake.

I am attempting to build a sub-project as a C static library, which then will be linked into another C++ project that creates an executable.

However, my cmake does not appear to work - I get "Unresolved External Symbol" for the function I am trying to use from the C library:

3>Main.cpp
3>Main.obj : error LNK2019: unresolved external symbol "void __cdecl mat_mul_c_driver(float (*)[2000],float (*)[1500],float (*)[1500])" (?mat_mul_c_driver@@YAXPEAY0HNA@MPEAY0FNM@M1@Z) referenced in function main
3>C:\src\testproj\build\Debug\MatMulCPP.exe : fatal error LNK1120: 1 unresolved externals

The definition in mat_mul_driver.c:

void mat_mul_c_driver(float(*m1)[M1COLS], float(*m2)[M2COLS], float(*result)[M3COLS])
{
    /* OpenCL structures */
    cl_device_id device;
    cl_context context;
    cl_program program;
...
(Not including the whole thing, it's a long function)

The top-level C++ cmake:

cmake_minimum_required(VERSION 3.1)
SET(CMAKE_TOOLCHAIN_FILE "C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake")

project(MatMulCPP LANGUAGES CXX)

add_executable(${PROJECT_NAME} Main.cpp)

add_subdirectory(mat_mul_c)

if(NOT TARGET MatMulC)
   message(SEND_ERROR "Attempt to link to non-existent library 'MatMulC'.")
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE MatMulC)
include_directories(mat_mul_c)

The C library cmake:

cmake_minimum_required(VERSION 3.1) # 3.1 << C_STANDARD 11

project(MatMulC LANGUAGES C)

find_package(OpenCL REQUIRED)

message("OpenCL_INCLUDE_DIR: ${OpenCL_INCLUDE_DIR}")
message("OpenCL_LIBRARY: ${OpenCL_LIBRARY}")

add_library(${PROJECT_NAME} STATIC mat_mul_driver.c mat_mul_driver.h)


target_link_libraries(${PROJECT_NAME} PRIVATE OpenCL::OpenCL)

set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 11
                                                 C_STANDARD_REQUIRED ON
                                                 C_EXTENSIONS OFF)

target_compile_definitions(${PROJECT_NAME} PRIVATE CL_TARGET_OPENCL_VERSION=300)

Attached are images of the file structure. (Note, the .cl is compiled by OpenCL, so does not need to be included in the cmake)

Top-level folder Sub-project folder

What am I doing wrong?

Tyler Shellberg
  • 1,086
  • 11
  • 28
  • 1
    You attempt to use C function in C++ code. Since the error message contains a function **signature**, you forgot to add `extern "C"` to the function declaration: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574420#12574420. (A function defined in C language doesn't produce signature in the object file.) – Tsyvarev May 25 '23 at 18:04

0 Answers0