Linker problem on VS2005 with VC++ - c++

Here's the scenario:
Platform:
VS2005 and language is VC++
Situation:
There's just 1 assembly CMPW32. It has 2 projects:
1 is a DLL project called CMPW32 and the 2nd one is an .exe project called Driver
They both share the same Debug folder under the main assembly folder.
I have been able to successfully export a few functions from the DLL. The Driver project accesses 1 of these exported functions. (First of all I am not if functions need to be exported for projects in the SAME assembly to be able to use them. I can just include the header files and use the functions I think.)
Following is are a few lines of code from some files which you might find useful to analyze my problem:
//main.cpp file from the Driver project which is meant to generate Driver.exe
#pragma comment(lib, "winmm.lib")
#include <CM.h>
#include "conio.h"
#include "CMM.h"
#include "CMF.h"
#define C_M_F _T("c:\\CannedMessages.en-US")
int_tmain (int argc, TCHAR* argv [])
{
CMM myobjModel;
CMF::Read (CANNED_MESSAGES_FILE, myobjModel);
getch();
}
//CMM.h file
#ifndef C_M_M
#define C_M_M
#include "CMD.h"
#include "CMC.h"
#include "CM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMM
{ //some code here...
}
//CMF.h
#ifndef C_M_F
#define C_M_F
#include "CMM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMF
{ //some code here...
}
//CMP.h
#ifndef C_M_P
#define C_M_P
#include "CMD.h"
#define C_M_B_F _T("CannedMessages.")
#ifdef _C_M_DLL
#define CM_DLL_API __declspec( dllexport )
#else
#define CM_DLL_API __declspec( dllimport )
#endif
extern "C"
{
//list of functions to be exported..
}
ERRORS on building the solution:
Error13 error LNK2019: unresolved external symbol "public: __thiscall CMM::~CMM(void)" (??1CMM##QAE#XZ) referenced in function _wmain main.obj
Error15 fatal error LNK1120: 2 unresolved externals C:\"somepath here which I cant disclose"\Projects\CMPW32\Debug\Driver.exe
Please Note: If I choose to build only the CMPW32 DLL project, there are no errors and the CMPW32.dll file gets generated in the debug folder with the correct functions getting getting exported.
However there seems to be some linking problem that is pretty evident and I don't know what's going on. I have included every required file and also have entered the required .lib in the input of the "Project Settings". The paths have been set correctly too.
It would be really helpful if someone could help me out with this. Please lemme know if additional information required.
Thanks,
Viren

Looks like your Driver.exe project does not include the CPP source files of the CMM class, likely CMM.cpp.
or
You have declare a destructor for CMM class in your .H file (CMM.H) and forgot to implement it in the .CPP file (CMM.CPP).

Related

How to add C library to Visual Studio project [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to add https://github.com/mlabbe/nativefiledialog this C library to my C++ project in visual studio 2022. I've built and generated the .lib file and added it to my project by going to Project->Properties->Linker. I added the path to this .lib file to Additional Library Directories. After doing this, I am still receiving "unresolved external symbol" linker errors. Specifically LNK2019 and LNK1120. I've also added the header file to the Additional Include Directories. What other steps am I missing?
Thanks for the help in advance!
EDIT:
Here is the code implementation:
StartupLayer.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Walnut/Application.h"
#include "ExcelReader.h"
#include "nfd.h"
#define BUFFER_SIZE 255
class StartupLayer : public Walnut::Layer {
private:
ExcelReader reader;
char inputPath[BUFFER_SIZE] = "enter folder";
public:
StartupLayer() : reader(ExcelReader()) {}
~StartupLayer() {}
virtual void OnUIRender() override;
};
StartupLayer.cpp
#include "StartupLayer.h"
void StartupLayer::OnUIRender() {
ImGui::Begin("Select Folder");
ImGui::InputText("select folder", inputPath, BUFFER_SIZE);
// when browse button is clicked
if (ImGui::Button("Browse")) {
nfdchar_t* outpath = NULL;
nfdresult_t result = NFD_OpenDialog(NULL, NULL, &outpath);
}
ImGui::End();
}
It's a Dear ImGui project, and I wanted to use this open file dialog library in one of my layers. The above code is that layer class, and I'm getting the unresolved external symbol on the NFD_OpenDialog function call.
C Code
The C app contains just a little function:
// ./code/c/lib.c
#include <stdio.h>
__declspec(dllexport) void f()
{
printf("\n This is a C code\n");
}
The keyword __declspec(dllexport) is only valid within the Microsoft compiler world and exports that function.
Compile C
Open the Visual Studio Developer Command Prompt and navigate to the folder, where the C project is. Then simply type: cl /LD lib.c. This will create two files. A lib.lib and a lib.dll.
C++ Code
Also nothing special. First you need the header file:
// ./code/cpp/Console/Console/Header.h
#pragma once
extern "C" {
__declspec(dllimport) void f();
}
You find declspec again, but this time it is specified with dllimport which makes perfectly sense as you are importing the function
Our app:
// ./code/cpp/Console/Console/Console.cpp
#include <iostream>
#include "Header.h"
#pragma comment(lib, "../../../c/lib.lib")
int main()
{
f();
}
Note the line #pragma comment(lib, "../../../c/lib.lib") which specifies the location of the .lib so the linker is able to reference the function f().
Execute
If you now execute Console.exe you will get an error because the dll can not be find. To fix this, just copy the lib.dll to the folder where Console.exe is.
Source.
I just forgot to add the path to the generated .lib file (nfd.lib) to Projects->Properties->Linker->Input Additional Dependencies. After doing this, everything worked perfectly.

build dlls using Visual Studio

I want to build dlls using Visual Studio which I want to use as functions in Matlab.
While doing so I have used declspec(dllimport) function...but I get compilation error LNK2019: unresolved external sumbol?
Appreciate your help.
#ifdef PRC50CONTROL_EXPORTS
#define PRC50CONTROL_API __declspec(dllexport)
#else
#define PRC50CONTROL_API __declspec(dllimport)
#endif
int PRC50CONTROL_API SetPRC50Gain(double inGain);
int PRC50CONTROL_API SetPRC50Gain(double inGain);
You have the return type before the __declspec()
Move it to
PRC50CONTROL_API int SetPRC50Gain(double inGain);
Also, obviously, make sure you have
PRC50CONTROL_EXPORTS
defined in your header of the library you're exporting the symbols from.

LNK4006 error in XLW 4.0.0.f0

XLW is an open sourced library that wrap C++ code into XLL add-in for Excel. A strange LNK4006 error is reported in a project i'm handling, reporting an XLW function is "already defined".
I'm using XLW version 4.0.0.f0, to simplify the situation, I break down it to a toy project, there are only three files --
StdAfx.cpp, the boilterplate file :
#include "stdafx.h"
StdAfx.h, boilterplate but mysterious (I never understood the content)
#if !defined(AFX_STDAFX_H__0D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_)
#define AFX_STDAFX_H_0_D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__0D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_)
and then the puzzle, WhyLNK4006.cpp:
#include <xlw/xlw.h>
void WhyLNK4006()
{
xlw::XlfExcel &x = xlw::XlfExcel::Instance();
xlw::XlfExcel::Instance().FreeMemory();
}
This will trigger an LNK4006 error, which I could suppress it as a warning but couldn't guess why this happen:
xlw-vc120-mt-gd-4_0_0f0.lib(XlfExcel.obj) : warning LNK4006: "public:
void __thiscall xlw::XlfExcel::FreeMemory(bool)"
(?FreeMemory#XlfExcel#xlw##QAEX_N#Z) already defined in B15.obj;
second definition ignored
I couldn't understand, why there's such an error?
Both XLW and my toy project are compiled as /MTd, all XLW header file XXX.h have preprocessor protection e.g.
> #if !defined(XXX_H)
> #define XXX_H //the read content of XXX.h
> #endif
How come this could lead to a double definition?

Using Vicon Datastream SDK with Unreal Engine throws error on namespace CPP in Vicons client.h

first of all I have to mention that I'm new to c++ but within my course of studies I have gained some experiences with programming.
Currently, I'm working on a plugin for a datastream between vicon blade 1.7 and unreal engine 4.4.3. This should be done by using the Vicon Datastream SDK v 1.4 which contains a header file, a library and a .dll file.
Right now, I'm having problems with compiling my basic plugin.
The Vicon DataStream SDK was build within an older version of visual studio than 2010. So I want to know if there is any possibility to go on working with the vicon sdk in visual studio 2013? Should I force the sdk to use the latest .dll in visual studio and if so how do I do that?
I already tried to go on working with the sdk ignoring the problem I've mentioned before.
When I built the project without changing the header file of the sdk I'm getting this error:
Error 2 error C2059: syntax error : 'constant'
Here are the affected rows:
#ifdef WIN32
#ifdef _EXPORTING
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif // _EXPORTING
#elif defined( __GNUC__ )
#if __GNUC__ < 4
#error gcc 4 is required.
#endif
#define CLASS_DECLSPEC __attribute__((visibility("default")))
#else
#define CLASS_DECLSPEC
#endif
#include <string>
namespace ViconDataStreamSDK
{
namespace CPP
{
...
}
}
If I redefine the second namespace to 'UCPP' I'm getting a huge list of errors like this one:
Error 2 error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __cdecl
ViconDataStreamSDK::UCPP::Client::Client(void)"
I think it's because CPP is already defined in unreal engine but because of the dependency of the header file to the .dll file in the sdk the definition of the namespace is unchangeable in the sdk.
Is that expectation correct or am I on the wrong track?
I had similar problems with the name space. To fix that I did this in my UE4 Plugin header file before including the Vicon DataStreamSDK
#define UCPP CPP
#undef CPP
#include <Client.h> //Vicon DataStreamSDK
.....
At the end of this file I redifined the CPP macro
#define CPP PCPP
This compiles and works fine with no problems

C++ Dll linking Curl

I am trying to create a dll which uses the curl library for a very simple function. Everything works just fine, the only problem is, that the curl linking does not seem to work properly.
I use the same linking, preprocessordefines and include directories like in my executable project where it works just fine so i am pretty sure it´s not about my linking or binary files of the libary.
Are there any special properties to link a libary to a dll?
My minimal sample code:
C++ Mainfile:
#include "main.h"
#include <Windows.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Info.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
namespace CurlDll
{
void CallHost::Try()
{
curlpp::Easy request;
request.setOpt(new curlpp::options::UserAgent("Mozilla/4.0"));
request.setOpt(new curlpp::options::AutoReferer(true));
request.setOpt(new curlpp::options::FollowLocation(true));
request.setOpt(new curlpp::options::Url("http://xml.utrace.de"));
request.perform();
MessageBox(0,"lololowwwwwwwwwwwl", "wqgqwwwwwgq", MB_OK |MB_ICONINFORMATION);
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason)
{return 1;}
Header file:
#ifdef MAIN_EXPORTS
#define MAIN_API __declspec(dllexport)
#else
#define MAIN_API __declspec(dllimport)
#endif
#include <iostream>
namespace CurlDll
{
class CallHost
{
public:
static MAIN_API void Try();
};
}
i get following linking errors #drescherjm
(47 , i will just post a few, i think that shouls be enough)
ERROR 2 error LNK2001: unresolved external Symbol
"__imp__WSAStartup#8".
ERROR 11 error LNK2001: unresolved external Symbol
"__imp__WSAGetLastError#0".
ERROR 33 error LNK2001: unresolved external Symbol
"__imp__setsockopt#20".
The linker errors are telling you that the linker cannot find definitions for these functions: WSAStartup, WSAGetLastError, setsockopt. These functions are defined in the import library Ws2_32.lib. You need to supply that import library to the linker.
This information is given in the documentation for the functions. For instance, the documentation for WSAStartup. At the bottom of the documentation topic is a table listing requirements. Note the required library, Ws2_32.lib.
The symbols WSAStartup, WSAGetLastError and setsocketopt are part of the Windows API, in Ws2_32.lib (e.g. see http://msdn.microsoft.com/en-gb/library/windows/desktop/ms742213(v=vs.85).aspx )
You should include ws2_32.lib as an additional library when you link your DLL. If you're using Visual Studio, it's likely that the search path should already find it; just add it as an additional library.
So actually, I suspect you're not using the exact same linker options compared to your .exe
If you're building a .exe or a .dll, the linker needs to ensure it can resolve ALL known symbols at link time.