You are here:

C++/LNK2001 Error: Unresolved external symbol

Advertisement


Question
QUESTION: Hi, sorry if you've gotten this twice, I realized an error in my email address!

I have bought a piece of hardware which I have all the appropriate .dll, .lib and header files for.  All the win32 test programs I've written for it compile and work with no problems.  However, having created a windows form interface for it (through MS VC++ 2008 SP1) I get the error:

LNK2001: unresolved external symbol "extern "C" short __stdcall Register_Card()".

This only happens when I compile the windows form versions, and when I comment out the method call Register_Card() it compiles with no problems with all the .dll, .lib and header files their and linked to properly.

Would very much appreciate any help you might offer...

Many thanks in advance - david.

ANSWER: Hi David,

The header file for Register_Card should have the following type of code:

#ifdef __cplusplus
extern "C" {
#endif

__declspec( dllimport ) void Register_Card();
// Other functions in the DLL

#ifdef __cplusplus
}
#endif

Depending the format and content of the H file, you can also use:

#ifdef __cplusplus
extern "C" {
#endif
#include RegisterCardHfile.h
#ifdef __cplusplus
}
#endif

Bill

---------- FOLLOW-UP ----------

QUESTION: Hi Bill!

Many thanks for your help on this, much appreciated.  I tried the change you recommended as

#ifndef _DASK_H
#define _DASK_H

#ifdef __cplusplus
extern "C" {
#endif

short __declspec(dllimport) Register_Card(usigned short CardType, unsigned short card_num);

// All the other imports...

#ifdef __cplusplus
}
#endif

#endif

However, the build still had probs, please see the output for the build below:

1>------ Rebuild All started: Project: Application, Configuration: Release Win32 ------
1>Deleting intermediate and output files for project 'Application', configuration 'Release|Win32'
1>Copying 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll' to target directory...
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Application.cpp
1>c:\Parameters.h(1062) : warning C4272: 'Register_Card' : is marked __declspec(dllimport); must specify native calling convention when importing a function.
1>AssemblyInfo.cpp
1>Compiling managed resources...
1>Read in 0 resources from "c:\Form1.resX"
1>Writing resource file...  Done.
1>Compiling resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>Application.obj : error LNK2001: unresolved external symbol "extern "C" short __clrcall Register_Card(unsigned short,unsigned short)" (?Register_Card@@$$J0YMFGG@Z)
1>C:\Release\Application.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Release\BuildLog.htm"
1>Application - 2 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

The strange thing is that the win32 console apps I've written all work perfectly with the __stdcall term, but it goes funny in the windows form application!

Would very much appreciate any insight you might offer into this...

Thanks - david.

Answer
Hi David,

You're in uncharted territory for me but from some research, the problem is there is a managed versus unmanaged class problem that can happen in .NET.  If you understand this article, I believe it contains the solution to your problem.

http://tinyurl.com/625plk

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.