-1

I want to build a small reusable library. I have 2 projects, project A to build a library function (must dynamic library), and project B to run tests on it.

My problem encountered the following error: In project B I used the following code:

 #include "StdAfx.h"
 #include "C:\......\projectA\Ent extension.h" (same folder with "Ent extension.cpp")
 void Call_plot()
 {
      ...=fent_select(..);
 }

however when I build the project I always get an error:

Error LNK2019 unresolved external symbol "class Ent __cdecl fent_select(wchar_t const ,enum Mode)" (?fent_select@@YAPEAVEnt@@PEB_WW4Mode@Db@@@Z) referenced in function "void __cdecl" Call_plot (?Call_plot@@YAXXZ)

How i can fix this problem?

Thanks you!

user207421
  • 305,947
  • 44
  • 307
  • 483
Yen Dang
  • 268
  • 2
  • 9
  • @StephenNewell: did I build projectA.lib the wrong way? I simply compile and add projectA.lib to the linker projectB – Yen Dang May 30 '21 at 03:16
  • You did not give enough information for anyone to provide more help than the duplicate that explains many of the common causes of this linker error. – drescherjm May 30 '21 at 04:43
  • Thanks all, turns out I was missing __declspec. now every function works as expected. – Yen Dang May 30 '21 at 08:16
  • Normally in native c++ using msvc you need a macro that evaluates to `__declspec(dllexport)` when building the dll and `__declspec(dllimport)` when using the dll. Related: [https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch](https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch) – drescherjm May 30 '21 at 14:05

1 Answers1

0

You need to link the object codes from both source code files together (and when you compile source code, it often turns into an object code). How to do this is somewhat dependent on your operating system and compiler. But, for example using GNU's gcc compiler, it would take the following compilation lines.

  1. Step 1, compile the base code you want include:
gcc -g -o Ent_extension.o Ent_extension.cpp
  1. Step 2, compile your code that wants to use the extension, telling it where the header file is for the base projectA using -I (include path):
gcc -I C:\......\projectA\ -g -o project_B.o project_B.cpp

But neither of these files is executable yet until you link them together, which takes the last step:

  1. Link them together into an executable:
gcc -o myprogram project_B.o C:\......\projectA\Ent_extension.o

Which will create myprogram out of both your code bases.

Note: the even more advanced way is to turn the projectA code into a library and link that in, but that should be a future task.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69