0

Im trying to call an API from a header file LG101API.h. I linked it in Leds.cpp like this #include "LG101API.h". The project has a dll and lib LG101API.dll LG101API.lib . I linked the lib in linker > input and added the path to the linker>general>additional Library directories. Now I keep on getting the

LNK2019 unresolved external symbol "__declspec(dllimport) int __stdcall KC_LG101_OPENPORT(int,int)" (__imp_?KC_LG101_OPENPORT@@YGHHH@Z) referenced in function _main

Here is the header code

#ifdef LG101API_EXPORTS
#define LG101API_API __declspec(dllexport)
#else
#define LG101API_API __declspec(dllimport)
#endif


// This class is exported from the LG101API.dll
class LG101API_API CLG101API {
public:
    CLG101API(void);
    // TODO: add your methods here.
};

extern LG101API_API int nLG101API;

//////////////////////////////////////////////////////////////////////////
LG101API_API int _stdcall KC_LG101_OPENPORT(int iCom, int iPort);
LG101API_API int _stdcall KC_LG101_CLOSEPORT(void);
LG101API_API int _stdcall KC_LG101_SETLED(int iLed, int iTurnOn);
LG101API_API int _stdcall KC_LG101_READLED(int* iLEDst);
LG101API_API int _stdcall KC_LG101_READGUARD(int* iGUARDst);
LG101API_API int _stdcall KC_LG101_SETLEDMODE(int iGroup, int iOnTm, int iOffTm);

//////////////////////////////////////////////////////////////////////////
LG101API_API int _stdcall KC_LG101_RESET();
LG101API_API int _stdcall KC_LG101_CONTROLLED(int iLed, int iOnOff, int iOnTm, int iOffTm);
LG101API_API int _stdcall KC_LG101_SETTWINKLE(int* iLedArray, int iOnTm, int iOffTm);
LG101API_API int _stdcall KC_LG101_SETBOOT(int* iLedArray, int* iTwinkleLedArray, int iOnTm, int iOffTm);
LG101API_API int _stdcall KC_LG101_READLEDST(int* iLedArray, int* iTwinkleLedArray);
LG101API_API int _stdcall KC_LG101_GETVERSION(int* iVer);

this is how im calling it in main

KC_LG101_OPENPORT(1, 9600);
    KC_LG101_SETLED(1, 1);
ProgZ_
  • 15
  • 7
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Jan 16 '20 at 07:34
  • no it didn't work – ProgZ_ Jan 16 '20 at 08:05

1 Answers1

0
  1. Look at .lib file by any viewer and find string (if you can't - there is troubles in library):

KC_LG101_OPENPORT@@YGHHH@Z

  1. Don't define LG101API_EXPORTS in your target software.
Evgeny
  • 1,072
  • 6
  • 6
  • If your .lib file doesn't contain KC_LG101_OPENPORT@@YGHHH@Z, but the string KC_LG101_OPENPORT is existed: use extern "c" for including header: extern "C" { #include "LG101API.h" } – Evgeny Jan 16 '20 at 07:17
  • `KC_LG101_OPENPORT@@YGHHH@Z ` is in the .lib file and for `extern "C" { #include "LG101API.h" }` when I use it it gives an` E0010 '#' not expected` here whatelse can I do? – ProgZ_ Jan 16 '20 at 08:19
  • I can't show exact code in comment style. So, separate code by lines: extern "C" { #include "LG101API.h" } – Evgeny Jan 16 '20 at 08:42
  • If "KC_LG101_OPENPORT@@YGHHH@Z" is in the .lib file. WITH EXACT "@@YGHHH@Z" tail - see case 2. – Evgeny Jan 16 '20 at 08:49
  • `` ÿÿL„ V)?KC_LG101_OPENPORT@@YGHHH@ZLG101API.dll LG101API.dll/ 1453363332 ` and I didn't define LG101API_EXPORTS IN THE TARGET SOFTWARe – ProgZ_ Jan 16 '20 at 09:03
  • It's strange. Last case: show diagnostic with linker command (in MS VS: tools - options - projects and solutions - build and run: set output verbosity to detailed) and you should see your library LG101API.lib in command line. I don't see other errors. – Evgeny Jan 16 '20 at 09:34
  • ` 1>Leds.obj : error LNK2019: unresolved external symbol __imp__KC_LG101_OPENPORT@8 referenced in function "void __cdecl ConnectLed(void)" (?ConnectLed@@YAXXZ) 1>Leds.obj : error LNK2019: unresolved external symbol __imp__KC_LG101_SETLED@8 referenced in function "void __cdecl ConnectLed(void)" (?ConnectLed@@YAXXZ) 1>F:\ajm\Components\KC-LG101 SDK release 20180316 EN\Leds\Debug\Leds.exe : fatal error LNK1120: 2 unresolved externals 1> 0 Warning(s) 1> 3 Error(s) 1> 1>Time Elapsed 00:00:00.14 === Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped === ` – ProgZ_ Jan 16 '20 at 09:43
  • this is what I got in the build tab – ProgZ_ Jan 16 '20 at 09:44
  • First of all, your error with __imp__KC_LG101_OPENPORT@8 (tail is "@8") it means you use extern "C" for header. According your previous comments you shouldn't use it. – Evgeny Jan 16 '20 at 09:57
  • Second: if you select detailed output, you should see something like this: link.exe /ERRORREPORT:PROMPT /OUT:"D:\programm\testcpp\Release\testcpp.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:"C:\boost\boost_1_63_0\lib32-msvc-14.0" kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' ......................... And you can see list of libraries: kernel32.lib user32.lib gdi32.lib ............ In your case, you should see your library. – Evgeny Jan 16 '20 at 09:59
  • The library is not shown in the build what does that mean? – ProgZ_ Jan 16 '20 at 11:10
  • Open properties of project. Select exact configuration Debug/Release, x86/x64. And (as you wrote early) Linker->Input->AdditionalDependencies - add your library. Most of similar errors are concerning the word "exact". – Evgeny Jan 16 '20 at 11:15