0

I have a simple C library that have following files: /testlib/library.h /testlib/library.lib

library.h contains:

#ifdef __cplusplus
extern "C" {
#endif

void function(int a, int b);

#ifdef __cplusplus
}
#endif

I'm trying to use this header in my main.cpp:

extern "C" {
#include "library.h"
}

int main()
{
    function(1, 2);

    return 0;
}

And here is my CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(StudyProject)

include_directories(testlib/)
link_directories(testlib/)

add_executable(${PROJECT_NAME} "main.cpp")

target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/testlib/)

I use Qt Creator (Windows platform, MinGW 7.3.0) and it builds project with following command:

cmake.exe --build . --target all

But i'm getting following error while linking:

undefined reference to 'function'

Can anyone help me what am I doing wrong?

  • What is the content of `library.cpp`? The linker error would suggest that you did not implement the function in your library or did not link to it. – Zaiborg Dec 15 '20 at 12:39
  • @Zaiborg, I do not have access to library source code. But I promise that it have all functions implementations, because Microsoft Visual C++ 2008 compiles project successfully when I add this library – 123Sparky321 Dec 15 '20 at 12:42
  • 1
    Why the `extern "C" { }` around `#include "library.h"`? You already have the `extern "C" { }` _in_ `library.h`. – Scheff's Cat Dec 15 '20 at 12:45
  • You can check with `dumpbin` what you `library.lib` offers. [SO: How to See the Contents of Windows library (*.lib)](https://stackoverflow.com/questions/305287/how-to-see-the-contents-of-windows-library-lib) – Scheff's Cat Dec 15 '20 at 12:49
  • @Scheff, Output has "SECT1 notype External | _function" and "UNDEF notype () External | _function" . Can you please explain what does it means? – 123Sparky321 Dec 15 '20 at 12:56
  • 2
    Don't you need to specify the name of the library being linked to, e.g. `target_link_directories(${PROJECT_NAME} library)` (where `library` is the base name of the library file)? – Ian Abbott Dec 15 '20 at 12:57
  • @Scheff Does it mean that i should add an underscore symbol to function in library.h? Looks very strange... – 123Sparky321 Dec 15 '20 at 13:04
  • Sorry, I cannot. (It looks like there is a function `_function` which is extern in `library.lib`.) Though, I found this: [DUMPBIN /SYMBOLS](https://learn.microsoft.com/en-us/cpp/build/reference/symbols?redirectedfrom=MSDN&view=msvc-160) – Scheff's Cat Dec 15 '20 at 13:04
  • If function `_function` is extern in `library.lib` I'm afraid that this won't eliminate your link error. – Scheff's Cat Dec 15 '20 at 13:06
  • Maybe, you should try this first with a C library you made by yourself - to have something reliable to experiment with. – Scheff's Cat Dec 15 '20 at 13:08
  • The line `target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/testlib/)` is definitely a **wrong way** for link with a library. You should link with a **library file** `library.lib`, not with a *directory* which contains it. Normally, the linker (or CMake itself) should complain about linking with a directory instead of a file. – Tsyvarev Dec 15 '20 at 13:56
  • @Tsyvarev I have changed it to ```target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/testlib/library.lib)```. Nothing changed. – 123Sparky321 Dec 15 '20 at 14:45
  • Looks like this issue: https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport – fabian Dec 15 '20 at 18:55

1 Answers1

0

Your cmake file does not say that you want to link your main program with the given library, so the linker rigthfully complains that function() is missing.

U. W.
  • 414
  • 2
  • 10