Compiling framework with a linked c++ object file
-
I am trying to compile a framework with the cl compiler and I am linking an object file which was written in c++. Problem is that everything compiles fine if I just declare a standard string in my object file but I get error messages when I initialize a std::string. I suspect I am missing the .lib files, is this correct? If yes, where can I find them?
Compiler Output:
LINK /nologo /NODEFAULTLIB /OPT:NOREF /NXCOMPAT /DynamicBase /out:lmflex.exe lmflex.obj C:\Users\ldeppler\Desktop\testobj.obj vendor_hostid.o bj lm_new.obj lmgr_trl.lib libsb.lib libcrvs.lib libredir_std.lib libredir_std.lib legacy_stdio_wide_specifiers.lib legacy_stdio_definitions.lib libv cruntime.lib libucrt.lib oldnames.lib kernel32.lib user32.lib netapi32.lib gdi32.lib comdlg32.lib comctl32.lib wsock32.lib shell32.lib Rpcrt4.lib oleaut32.lib Ole32.lib Wbemuuid.lib wintrust.lib crypt32.lib Ws2_32.lib iphlpapi.lib Psapi.lib advapi32.lib Shlwapi.lib dhcpcsvc.lib userenv.lib atls. lib libcmt.lib lmgr_dongle_stub.lib .\activation\lib\libnoact.lib testobj.obj : error LNK2019: Verweis auf nicht aufgel÷stes externes Symbol ""void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ)" in Funkti on ""void * __cdecl std::_Allocate(unsigned __int64,unsigned __int64,bool)" (?_Allocate@std@@YAPEAX_K0_N@Z)". testobj.obj : error LNK2019: Verweis auf nicht aufgel÷stes externes Symbol ""void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error@std@@YAX PEBD@Z)" in Funktion ""public: static void __cdecl std::basic_string,class std::allocator >::Xlen(void)" (? Xlen@?char_traits@D@std@@V?$allocator@D@2@@std@@SAXXZ)". lmflex.exe : fatal error LNK1120: 2 nicht aufgel÷ste Externe
Here is my .cpp file
#define _CRT_SECURE_NO_WARNINGS #include <winsock2.h> #include <limits.h> #include <Windows.h> #include <string> #include <sstream> #include <iphlpapi.h> #include <stdio.h> #include <sstream> #include <iostream> #include <stdlib.h> #include <vector> #include <AclAPI.h> #include <WinBase.h> #include <Sddl.h> #include <Windows.h> #include <DSRole.h> #include <codecvt> #include <atlbase.h> #include <tchar.h> #include <FileAPI.h> #include <WinIoCtl.h> #include <WinIoCtl.h> #include "compositeit4e.h" #include <fstream> #include <iomanip> #include <atlstr.h> #include <algorithm> #include <atlbase.h> #pragma comment(lib, "netapi32.lib") #pragma comment(lib, "Advapi32.lib") #pragma comment(lib, "IPHLPAPI.lib") #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) #define INFO_BUFFER_SIZE 32767 TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; TCHAR szGroupName[UNLEN]; TCHAR szDomainName[DNLEN]; DWORD cchGroupName = UNLEN; DWORD cchDomainName = DNLEN; SID_NAME_USE Use; DSROLE_PRIMARY_DOMAIN_INFO_BASIC * info; DWORD dwLogicalDrives, x; TCHAR szRoot[32]; SHFILEINFO shInfo; DISK_GEOMETRY pdg = { 0 }; // disk drive geometry structure BOOL bResult = FALSE; // generic results flag ULONGLONG DiskSize = 0; // size of the drive, in bytes using std::endl; #ifdef _UNICODE typedef std::wstring tstring; #define tcout std::wcout #define tcin std::wcin #else typedef std::string tstring; #define tcout std::cout #define tcin std::cin #endif const char* getComputername() { //vector for the computername std::string computername ="hello"; std::string myComputerName; const char* new_string; //get and print computername if (GetComputerName(infoBuf, &bufCharCount)) { //save computername to string //std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; //myComputerName = myconv.to_bytes(infoBuf); //computername = static_cast<wchar_t>(bufCharCount); //WideCharToMultiByte(CP_ACP, 0, infoBuf, wcslen(infoBuf) + 1, new_string, sizeof(new_string), NULL, NULL); //new_string = new char[computername.length() +1]; //strcpy(new_string, computername.c_str()); new_string = reinterpret_cast<const char*>(infoBuf); } bufCharCount = 32767; return new_string; }
Here is my header file: #ifdef __cplusplus extern "C" { #endif const char* getComputername(); #ifdef __cplusplus } #endif
Here is part of my .c file with the includes and the function which calls the function of my object file:
#include "lmclient.h" #include "lm_attr.h" #include "string.h" #include "compositeit4e.h" x_flexlm_gethostid(idtype) short idtype; { HOSTID *h = l_new_hostid(); memset(h, 0, sizeof(HOSTID)); if (idtype == VENDEF_ID_TYPE) { h->type = VENDEF_ID_TYPE; const char* testComposite = getComputername(); strncpy(h->id.vendor, testComposite, MAX_HOSTID_LEN); /* LONGNAMES */ h->id.vendor[MAX_HOSTID_LEN] = 0; return(h); } return((HOSTID *) NULL); }
By the way my .obj file is compiled liked that:
cl /EHsc /c /MT /I. testobj.cpp
-
Sounds like one of these happened:
* You are trying to link libraries that were built using sufficiently different compiler settings. Could for example be a conflict between debug and release versions.
* You are missing a library. The NODEFAULTLIB looks suspicious, as std::bad_alloc is something from the C++ standard library. I am not 100% certain what exactly this linker parameter does (as I do not use MSVC) but it sure sounds like it could be the cause of your troubles.PS: You are obviously using a German locale for your linker and you named yourself luce_de. This is a German speaking forum.
-
SeppJ schrieb:
This is a German speaking forum.
Jo, aber Stackoverflow nicht, und warum sollte man sich auch die Mühe machen, eine Frage extra nochmal zu übersetzen?