-
February 23rd, 2011, 06:10 PM
#1
Linker Error while linking .lib files "nresolved external symbol"
Hi guys,
I have project named TestingDLL(Win32 DLL project). It has the following header file
(The code has been taken from a site which I dont remember but for discussion purpose this is fine)
sampledll.h
---------------
#ifndef INDLL_H
#define INDLL_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void HelloWorld() ;
#else
extern __declspec(dllimport) void HelloWorld() ;
#endif
#endif
and a .cpp file
sampldll.cpp
---------------
#define EXPORTING_DLL
#include "sampledll.h"
#include <stdio.h>
void HelloWorld()
{
printf("hello");
}
I have build this project and I got the TestingDLL.lib sucessfully after the build.
I have another project CallingDLL(Win 32 Console application) whose code is as follows
calling.cpp
--------------
#include "sampledll.h"
#pragma comment(lib,"TestingDLL.lib")
int main()
{
HelloWorld();
return 0;
}
this code calls the library function Helloworld(). Now I went to projects->settings->link->input
and specified the .lib file name and the path to find it. I also made chages in C/C++->preprocessor->Additional include directories and added the path where sampledll.h is found.
I always get
compiling...
calling.cpp
Linking...
calling.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl HelloWorld(void)" (__imp_?HelloWorld@@YAXXZ)
Debug/CallingDll.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
CallingDll.exe - 2 error(s), 0 warning(s)
I have also tried some other ways like going to tools->options->Directories and adding the paths but still I get the same error.
It would be great help if someone tells me in a systematic manner on how to link a .lib file to another project.
Thanks
-
February 24th, 2011, 04:50 AM
#2
Re: Linker Error while linking .lib files "nresolved external symbol"
-
February 24th, 2011, 08:12 AM
#3
Re: Linker Error while linking .lib files "nresolved external symbol"
seems to be you did it in correct manner, may be you missing proper path.. check all settings once again..
-
February 24th, 2011, 08:32 AM
#4
Re: Linker Error while linking .lib files "nresolved external symbol"
When DLL (und lib) is created (compiled( the EXPORTING_DLL is defined. So the type of the HelloWorld() is
Code:
extern __declspec(dllexport) void HelloWorld() ;
But if EXPORTING_DLL is not defined in exe project then HelloWorld() would be declared as
Code:
extern __declspec(dllimport) void HelloWorld() ;
And it seems to be the case here: the linker in exe project looks for "__declspec(dllimport) void __cdecl HelloWorld(void)" while the lib contains only "__declspec(dllexport) void __cdecl HelloWorld(void)"
Victor Nijegorodov
-
February 24th, 2011, 10:40 AM
#5
Re: Linker Error while linking .lib files "nresolved external symbol"
Hi guys
thanks for the replies. I havent got rid of the previous problem but I tried a new toy project to understand the linking. It is as follows
code for dll
example.cpp
---------------
/ Example.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
// This function is used to calculate the total area of a parallelepiped rectangle
double BoxArea(double L, double H, double W);
// This function is used to calculate the volume of a parallelepiped rectangle
double BoxVolume(double L, double H, double W);
// This function is used to get the dimensions of a rectangular parallelepiped
// calculate the area and volume, then pass the calculated values to the
// function that called it.
extern "C" __declspec(dllexport)void BoxProperties(double Length, double Height,
double Width, double& Area, double& Volume);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
double BoxArea(double L, double H, double W)
{
return 2 * ((L*H) + (L*W) + (H*W));
}
double BoxVolume(double L, double H, double W)
{
return L * H * W;
}
void BoxProperties(double L, double H, double W, double& A, double& V)
{
A = BoxArea(L, H, W);
V = BoxVolume(L, H, W);
}
I build the project and there were no errors and I got the .lib and .dll files upon building the project.
There is another project where I am trying to use the .lib generated above. I have copied the Example.lib, Example.dll and Example.obj to the new project folder . I also added these files to the project. In the project->settings->link->input I appended Example.lib to the Object/Library module text box . I wrote the following cpp code in the new project.
test.cpp
-----------
#include <iostream>
extern "C" __declspec(dllimport)void BoxProperties(double L, double H,
double W, double& A, double& V);
int main()
{
double Length, Height, Width, Area, Volume;
cout << "Enter the dimensions of the box\n";
cout << "Length: ";
cin >> Length;
cout << "Height: ";
cin >> Height;
cout << "Width: ";
cin >> Width;
BoxProperties(Length, Height, Width, Area, Volume);
cout << "\nProperties of the box";
cout << "\nLength: " << Length;
cout << "\nHeight: " << Height;
cout << "\nWidth: " << Width;
cout << "\nArea: " << Area;
cout << "\nVolumne: " << Volume;
cout << "\n\n";
return 0;
}
After building the code I get the following linker error(no previous linker errors ):
--------------------Configuration: Exampletest - Win32 Debug--------------------
Linking...
Creating library Debug/Exampletest.lib and object Debug/Exampletest.exp
LIBCMTD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Exampletest.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Exampletest.exe - 2 error(s), 0 warning(s)
Guys help me resolve this
Thanks
-
February 24th, 2011, 10:54 AM
#6
Re: Linker Error while linking .lib files "nresolved external symbol"
Please, edit your post adding Code tags around code snippets. Otherwise your code is absolutely unreadable!
Victor Nijegorodov
-
March 1st, 2011, 10:16 AM
#7
Re: Linker Error while linking .lib files "nresolved external symbol"
Thanks for the suggestion Victor! I will take care of that in my future posts.
Well thanks for all the help you guys have done. I finally resolved my issues regarding linker error. I will update this post with step by step procedure I figured out to make a dll and use it in other applications.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|