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?
Related
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 4 years ago.
Improve this question
So I'm a complete noob to C++ and I'm trying to make a simple "Hello world" program for an assignment. My code is below:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout<<"hello world!";
return 0;
}
For some reason, VS2017 is throwing an error at cout, saying it's undefined. I've done some reading on old posts about this and added in #include "stdafx.h to see if that would solve it as per old advice, but it continues to give me the error. Any ideas?
EDIT:
Again a complete noob but there are multiple version of stdafx.h that come up when I search for it, here's what looks like the "main" one:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#pragma once
#ifndef STRICT
#define STRICT
#endif
#include "targetver.h"
[!if SERVICE_APP]
#define _ATL_FREE_THREADED
[!else]
#define _ATL_APARTMENT_THREADED
[!endif]
#define _ATL_NO_AUTOMATIC_NAMESPACE
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
[!if PREVIEW_HANDLER || THUMBNAIL_HANDLER || SEARCH_HANDLER]
#ifdef _MANAGED
#error File type handlers cannot be built as managed assemblies. Set the Common Language Runtime options to no CLR support in project properties.
#endif
#ifndef _UNICODE
#error File type handlers must be built Unicode. Set the Character Set option to Unicode in project properties.
#endif
#define SHARED_HANDLERS
[!endif]
[!if SUPPORT_MFC]
#include <afxwin.h>
#include <afxext.h>
#include <afxole.h>
#include <afxodlgs.h>
#include <afxrich.h>
#include <afxhtml.h>
#include <afxcview.h>
#include <afxwinappex.h>
#include <afxframewndex.h>
#include <afxmdiframewndex.h>
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
[!endif]
[!if SUPPORT_COMPLUS]
#include <comsvcs.h>
[!endif]
#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW
#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
error C2065: 'cout': undeclared identifier is a result of the absence of #include <iostream>. The first cause might be stdafx.h content. The one you provided, I'm not so sure how it is related to your main.cpp/project. Let's start from a fresh project: ...VS2017 IDE: Create new project, ConsoleApplication project-type, & replace the main() function with yours.
A VS2017 IDE (15.8.2) fresh ConsoleApplication project: ConsoleApplication1
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hello world!";
return 0;
}
stdafx.h: (Generated by the IDE)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
// TODO: reference additional headers your program requires here
stdafx.cpp: (Generated by the IDE)
// stdafx.cpp : source file that includes just the standard includes
// ConsoleApplication1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
targetver.h: (Generated by the IDE)
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
** This code runs perfectly. **
--
"there are multiple versions of stdafx.h that come up when I search for it" - What do you mean? In your project? On the internet? You can't just take one stdafx.h from the internet. stdafx.h content is tailored per project, not universal. What I provided above, for example, is the IDE default new ConsoleApplication project stdafx.h. You may add to the file according to your project needs.
I try to compile a very simple dynamic library project as .dll file.
The name of the project is "Library".
I'm using Visual Studio 2015 and the project properties are these:
Debug Properties
Release Properties
In the project there are two files only: ClassA.h and ClassA.cpp.
The code in ClassA.h is:
#ifndef CLASSA_H
#define CLASSA_H
using namespace std;
#ifdef LIBRARY_EXPORTS
#define CLASSA_API __declspec(dllexport)
#else
#define CLASSA_API __declspec(dllimport)
#endif
class ClassA
{
public:
static CLASSA_API void func();
};
#endif
The code in ClassA.cpp is:
#include "ClassA.h"
#include <iostream>
void ClassA::func()
{
cout << "SUCCESS!" << endl;
}
When I try to compile this project I receive this error:
Severity Code Description Project File Line Error LNK1561 entry point
must be defined Library C:\Users\UX303\Documents\Visual Studio
2015\DLLTest\Library\LINK 1
It is likely that your configuration is not right.
Be sure to double check your "Active Configuration" (Debug / Release), to see if you are really building a DLL.
I have just made such a mistake, and came across this question.
In 64 bit machine I was getting same error when 'Solution Platforms' set to 'x86'. The error goes away when I set the 'Solution Platforms' to 'x64'.
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
I'm pretty rusty in C++, so forgive me for any stupid comments/questions. Right now I'm working in Microsoft Visual C++ 2010 Express. I have two files - a source and a header - and VS is recognizing the header file when I include it, but it cannot find any definitions from within the header file. It shows me 'Error: identifier "RAW_PACKET_SIZE" is undefined'. The code was provided as a sample to work with a device's API, so it should work. I'm assuming the problem is with the VS setup. Here's some intro code form each:
recorder.cpp
#include <vector>
#include "APIW32.h"
#pragma comment(lib,"APIW32.lib")
int devID;
float* buf = new float[RAW_PACKET_SIZE]; // error is here, at 'RAW_PACKET_SIZE'
APIW32.h
#pragma once
#ifdef EXPORTS
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
#define MIN_BW 0.301003456
#define MAX_BW 10100000
#define RAW_PACKET_SIZE 299008
UPDATE:
It appears that the error was only appearing in Intellisense, and not as an actual build error. Moral of the story - Intellisense is not always right!
Try float* buf = new float[RAW_PACKET_SIZE];
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).