0

I have to c/c++ projects - one a static library and the other is on exe. I have a header file visible to both with this declaration

void SendChar(void);

I implement this in the static library

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

void SendChar(void)
{
   printf("SendChar\n");
}

And use it in the exe

#include "USART.h"

extern void SendChar();

int main()
{
    std::cout << "Hello World!\n";
    SendChar();
}

I get a linker error and I dont understand why

1>EncoderApp.obj : error LNK2019: unresolved external symbol "void __cdecl SendChar(void)" (?SendChar@@YAXXZ) referenced in function _main
1>C:\Users\Graham.Labdon\Documents\IPCSolution\Debug\EncoderApp.exe : fatal error LNK1120: 1 unresolved externals

Please can someone explain this

Gunner
  • 125
  • 10
  • 1
    Make sure to include all C files and compiled libraries in your project (or linker-instructions file). BTW, why did you choose not to keep a consistent coding convention across your code, with `void SendChar(void)` in one place and`void SendChar()` in another? – goodvibration Oct 22 '19 at 09:59
  • They are both C++ files, right? And the exe project depends on the static library project, right? – user253751 Oct 22 '19 at 10:00
  • If you are correctly linking to the static lib, and even then you are getting this error , that means its due to name mangling and you need extern C. See https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c – Nitesh Oct 22 '19 at 10:08
  • Set the dependency between .exe and .lib project. Right-click on .exe project > Project Dependencies... > make sure .exe depends on .lib. This check mark is "magical", because .exe's linker will automatically include the .lib. This magic does not work between .exe and .dll, and there you have to manually add the corresponding .lib to .exe's linker. – Dialecticus Oct 22 '19 at 10:10
  • exe includes static library via linker input settingd – Gunner Oct 22 '19 at 10:11
  • 1
    Is SendChar defined in .c or .cpp file? If it is in .c file then you must declare it in .exe project as `extern "C"` instead of just `extern`. – Dialecticus Oct 22 '19 at 10:15
  • Maybe you include some other .lib with same name but different path. – Dialecticus Oct 22 '19 at 10:17

0 Answers0