I'm trying to compile a solution in MS Visual Studio C++ 2012.
My code uses marshallsoft AES library.
I added these for library and include paths:
C:\aes4c\APPS to Configuration properties->VC++ Directories->Include Directories
C:\aes4c\DLLS to Configuration properties->VC++ Directories->Library Directories
When I compile the individual .cpp file it compiles without problem but when I build the solution I get:
------ Build started: Project: cryptest2, Configuration: Debug Win32 ------
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesAttach#8 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesEncryptFile#12 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesInitAES#20 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
C:\Users\ariyan\documents\visual studio 2012\Projects\cryptest2\Debug\cryptest2.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is the problem?
How Can I fix it?
My code is:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "aes.h"
int EncryptFile(char *KeyBuffer, char *FileName);
int _tmain(int argc, _TCHAR* argv[])
{
EncryptFile("1234567890abcdef","c:\test.txt");
return 0;
}
int EncryptFile(char *KeyBuffer, char *FileName)
{int Code;
// attach DLL
Code = aesAttach(0, 0);
if(Code<0)
{printf("ERROR %d: Cannot attach\n", Code);
return FALSE;
}
printf("Will encrypt file in CBC mode\n");
Code = aesInitAES((char *)KeyBuffer, NULL, AES_ECB_MODE, AES_ENCRYPT, NULL);
if(Code<0)
{printf("aesInitAES fails\n");
return FALSE;
}
printf("Encrypt file...\n");
Code = aesEncryptFile(NULL, KeyBuffer, FileName);
if(Code<0)
{printf("aesEncryptFile fails\n");
return FALSE;
}
printf("%d bytes encrypted\n", Code);
return Code;
}
It's not enough to add to library path - that just tells the linker where to look for a library if and when it decides to link with it. But you have to tell the linker to look for it in the first place. For that, mention the LIB file name in
Project > Properties > Linker > Input > Additional Dependencies
Related
After trying to build this simple program:
#include <iostream>
#include <cstdlib>
using namespace std;
void main()
{
int a = 6;
cout << a << endl;
}
I get many "unresolved external symbol" errors:
LNK1120 12 unresolved external errors
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol strcat_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure##YAXPEAXPEBD#Z) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_error_.obj) 1
Error LNK2019 unresolved external symbol strcpy_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure##YAXPEAXPEBD#Z) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_error_.obj) 1
Error LNK2019 unresolved external symbol wcscpy_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath##YAHPEB_WPEA_W_K#Z) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
Error LNK2019 unresolved external symbol _CrtDbgReport referenced in function _CRT_RTC_INIT CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_init_.obj) 1
Error LNK2019 unresolved external symbol _CrtDbgReportW referenced in function _CRT_RTC_INITW CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_init_.obj) 1
Error LNK2019 unresolved external symbol _wmakepath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath##YAHPEB_WPEA_W_K#Z) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
Error LNK2019 unresolved external symbol _wsplitpath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath##YAHPEB_WPEA_W_K#Z) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
Error LNK2001 unresolved external symbol __C_specific_handler CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_error_.obj) 1
Error LNK2019 unresolved external symbol __stdio_common_vsprintf_s referenced in function _vsprintf_s_l CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_error_.obj) 1
Error LNK2019 unresolved external symbol __vcrt_GetModuleFileNameW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll##YAPEAUHINSTANCE__##XZ) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
Error LNK2019 unresolved external symbol __vcrt_GetModuleHandleW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll##YAPEAUHINSTANCE__##XZ) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
Error LNK2019 unresolved external symbol __vcrt_LoadLibraryExW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll##YAPEAUHINSTANCE__##XZ) CPP C:Documents\VSCPP\CPP\CPP\MSVCRTD.lib(_pdblkup_.obj) 1
What is causing this and how this can be fixed?
To reproduce the issue,
Create a project > VC++ > CLR > Class library, name the project "ClassLibrary2" and use below two files as is.
// ClassLibrary2.h
#pragma once
#include <msclr\marshal.h>
#include <ShObjIdl.h>
using namespace System;
namespace ClassLibrary2 {
public ref class Class1
{
public : void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext);
};
}
// ClassLibrary2.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "ClassLibrary2.h"
void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext)
{
msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
PCWSTR pszStr = context->marshal_as<const wchar_t*>(ext);
IEnumAssocHandlers *pEnumHandlers = NULL;
/* below line is producing problems */
SHAssocEnumHandlers(pszStr, ASSOC_FILTER_RECOMMENDED, &pEnumHandlers);
}
Now, if I build the project, I get following errors :
Error 2 error LNK2028: unresolved token (0A0000A2) "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
Error 3 error LNK2019: unresolved external symbol "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
How to remove those linker errors ?
I was trying to poll available devices on a Windows machine by this code example from Microsoft. But there are link errors that relates to _main().
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
For some reasons, there are link errors:
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList#4 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetDeviceRegistryPropertyW#28 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiEnumDeviceInfo#12 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetClassDevsW#16 referenced in function _main
1>c:\users\visual studio 2010\Projects\usb\Debug\usb.exe : fatal error LNK1120: 4 unresolved externals
What they are about? There is no _main() at all.
First thing first, the linker errors are not about missing main, but about other functions that are referenced (called) from main.
The functions you mentioned in linker errors are from library: Setupapi.lib, and you need to include it in Linker settings (Input) of your project.
Why did it worked on VS re-open?
Probably simply because you changed the configuration (by mistake). For example from Win32 to x64, and/or from Debug to Release (or any combination) of these. The other configuration didn't have reference to this library added.
Im totally new to google testing in CLI. Just managed to set up and integrate google test with visual studio 2012. However, when I try to include the header file of my project with my tester.h file (because I want to test the functions in that headerfile), I encountered the following error:
Error 2 error LNK2028: unresolved token (0A0006FB) "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jacky\Desktop\EzXpns3\test_project\main.obj test_project
Error 3 error LNK2019: unresolved external symbol "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jia Wei\Desktop\EzXpns3\test_project\main.obj test_project
I have tried including dependencies gtestd.lib, kernel32.lib, user32.lib, advapi32.lib, Ws2_32.lib
Problem slightly resolved. Instead of having expenseTracker.h and ExpenseTracker.cpp, I placed all my implementation of ExpenseTracker.h in the header file itself and everything compiled nicely and test run. However, my entire project has been built on both header file and cpp file and its not very wise to redo everything for testing purposes right? Could anyone help with this?
Attached below are my header files.
//tester.h
#include "gtest/gtest.h" //include to use Google Unit test's stuff
#include "C:\Users\Jacky\Desktop\EzXpns3\Source - testing\EzXpns2\ExpenseTracker.h"
using namespace std;
class ExpenseTracker;
/************************************************************************/
/* We write test cases here */
/************************************************************************/
TEST(basic_test, add_simple_route)
{
ASSERT_EQ(1, 1);
}
TEST(enter_settings, user_login)
{
ExpenseTracker :: ExpenseTracker();
//loadUserInfo();
string username = "XXX_XXX";
string password = "12345";
//myTracker -> loadUserInfo();
//bool result = myTracker -> login(username, password);
//ASSERT_EQ(true, result);
}
void runTest(int argument_count, char** argument_vars)
{
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar(); //pause the program after all the testing
}
//main.cpp
#include "tester.h"
#include <cstdio>
using namespace System;
using namespace testing;
int main(int argument_count, char** argument_vars)
{
//int argc;
//char** argv;
//runTest(argc, argv);
//InitGoogleTest(argc, argv);
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar();
return 0;
}
//ExpenseTracker.h, the file which I want to include
#ifndef _EXPENSETRACKER_H
#define _EXPENSETRACKER_H
#include <string>
#include <vector>
#include "user2.h"
using namespace std;
class ExpenseTracker
{
private:
vector<User*> allMyUsers;
public:
ExpenseTracker(); //empty constructor
void addUser(User*);
int findUser(string);
bool login(string, string);
void loadUserInfo();
User* getUser(string);
int getUserSize();
};
#endif;
You need to provide an implementation for the constructor. In other words, you should change:
ExpenseTracker(); //empty constructor
into:
ExpenseTracker() {} // empty constructor
However, since the constructor is empty you might as well remove it entirely!
I have a simple logger that is implemented as a singleton. It works like i want when I compile and run it with g++ in linux but when I compile in Visual Studio 9.0 with vc++ I get the following errors. Is there a way to fix this? I don't mind changing the logger class around, but I would like to avoid changing how it is called.
1>Linking...
1>loggerTest.obj : error LNK2005: "public: static class Logger * __cdecl Logger::getInstance(void)" (?getInstance#Logger##SAPAV1#XZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::log(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?log#Logger##QAEXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::closeLog(void)" (?closeLog#Logger##QAEXXZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "private: static class Logger * Logger::_instance" (?_instance#Logger##0PAV1#A) already defined in Logger.obj
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
The code, three files Logger.h Logger.cpp test.cpp
#ifndef __LOGGER_CPP__
#define __LOGGER_CPP__
#include "Logger.h"
Logger* Logger::_instance = 0;
//string Logger::_path = "log";
//ofstream Logger::_log;
//boost::mutex Logger::_mutex;
Logger* Logger::getInstance(){
{
boost::mutex::scoped_lock lock(_mutex);
if(_instance == 0) {
_instance = new Logger;
_path = "log";
}
} //mutex
return _instance;
}
void Logger::log(const std::string& msg){
{
boost::mutex::scoped_lock lock(_mutex);
if(!_log.is_open()){
_log.open(_path.c_str());
}
if(_log.is_open()){
_log << msg.c_str() << std::endl;
}
}
}
void Logger::closeLog(){
Logger::_log.close();
}
#endif
` ...
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <iostream>
#include <string>
#include <fstream>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
using namespace std;
class Logger {
public:
static Logger* getInstance();
void log(const std::string& msg);
void closeLog();
protected:
Logger(){}
private:
static Logger* _instance;
static string _path;
static bool _logOpen;
static ofstream _log;
static boost::mutex _mutex; //check mutable
};
#endif
test.cpp
`
#include <iostream>
#include "Logger.cpp"
using namespace std;
int main(int argc, char *argv[])
{
Logger* log = Logger::getInstance();
log->log("hello world\n");
return 0;
}
This is a problem because you've defined the symbol multiple times by compiling the CPP then also including it in test.cpp... By convention you should only include the declaration, not the definition as you've done.
I am surprised that gcc would allow one to be so lax about this.
change
#include "Logger.cpp"
to
#include "Logger.h"
and give it a try.
The problem is because you have done #include "Logger.cpp" instead of #include "Logger.h" in test.cpp . Because of this, the symbols inside the Logger.cpp will be defined multiple times (once for the translation unit logger.cpp and once for test.cpp). Having multiple inclusion guard doesn't help because it works only within a translation unit.