I have downloaded source and built LLVM using CMake and VS 2015.
My sample program is the following:
#include <iostream>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/CFG.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
int main(int argc, char **argv) {
if (argc < 2) {
errs() << "Usage: " << argv[0] << " <IR file>\n";
return 1;
}
// Parse the input LLVM IR file into a module.
SMDiagnostic Err;
LLVMContext TheContext;
std::unique_ptr<Module> Mod = parseIRFile(argv[1], Err, TheContext);
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}
// ... use module
}
I get the following linking errors after having included the LLVM lib directory in the linker library path:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall llvm::LLVMContext::LLVMContext(void)" (??0LLVMContext#llvm##QAE#XZ) referenced in function _main LLVM_IRReader c:\Users\nlykkei\documents\visual studio 2015\Projects\LLVM_IRReader\LLVM_IRReader\Main.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall llvm::Module::~Module(void)" (??1Module#llvm##QAE#XZ) referenced in function "public: void * __thiscall llvm::Module::`scalar deleting destructor'(unsigned int)" (??_GModule#llvm##QAEPAXI#Z) LLVM_IRReader c:\Users\nlykkei\documents\visual studio 2015\Projects\LLVM_IRReader\LLVM_IRReader\Main.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "class llvm::raw_ostream & __cdecl llvm::errs(void)" (?errs#llvm##YAAAVraw_ostream#1#XZ) referenced in function _main LLVM_IRReader c:\Users\nlykkei\documents\visual studio 2015\Projects\LLVM_IRReader\LLVM_IRReader\Main.obj 1
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall llvm::SMDiagnostic::print(char const *,class llvm::raw_ostream &,bool,bool)const " (?print#SMDiagnostic#llvm##QBEXPBDAAVraw_ostream#2#_N2#Z) referenced in function _main LLVM_IRReader c:\Users\nlykkei\documents\visual studio 2015\Projects\LLVM_IRReader\LLVM_IRReader\Main.obj 1
Does anyone know how to resolve these errors? I see, e.g., raw_ostream.obj in the LLVM build directory...
I've never used LLVM but based on your error messages, the linker didn't succeed in finding the LLVM class implementations.
To my point of view, including the LLVM lib directory is not enough. You have to include also the LLVM libraries (.lib which implement the LLVM classes) you want to link with.
Related
I am working on a C++ Windows Form Application in Visual Studio 2019.
I want to use some functions of the external library EDFlib. EDFlib is a programming library for C/C++ which consists of only two files (.c and .h).
Here, a simple code that writes data in an EDF file when I click on a button:
#include "edflib.h"
// [...]
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
double buf[200];
for (int i = 0; i < 200; i++)
buf[i] = i;
int hdl;
hdl = edfopen_file_writeonly("test.edf", EDFLIB_FILETYPE_EDFPLUS, 1);
// [...]
edfwrite_physical_samples(hdl, buf);
edfclose_file(hdl);
}
// [...]
I have the 2 following errors:
Erreur LNK2028 jeton non résolu (0A000038) "extern "C" int __cdecl edfopen_file_writeonly(char const *,int,int)" (?edfopen_file_writeonly##$$J0YAHPBDHH#Z) référencé dans la fonction "private: void __clrcall TestEDF::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#TestEDF##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
&
Erreur LNK2019 symbole externe non résolu "extern "C" int __cdecl edfopen_file_writeonly(char const *,int,int)" (?edfopen_file_writeonly##$$J0YAHPBDHH#Z) référencé dans la fonction "private: void __clrcall TestEDF::MyForm::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click#MyForm#TestEDF##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
I tried to read documentation about these errors.
But I am not an C/C++ expert. I don't know what to do. I need some help.
Thanks.
I am guessing the errors state that you have unresolved symbols,
it means that you are calling functions that you have a declaration for (from the #include "edflib.h") that the linker tries to find during the build process but have no definition.
it seems like you just included the header of the library, but you need to also link with it.
I don't know what IDE you use but you should look up how to link libraries to your projects in C++.
there is a lot of information about creating and linking libraries online that should help you solve this issue.
You Should Give Extern "C" Method and Add the Library into It, Check
https://isocpp.org/wiki/faq/mixing-c-and-cpp
You Cannot Externally Add C Libraries in C++
I'm trying, without much luck, to add chm options to an existing project.
Main.cpp has this:
#include "HtmlHelp.h"
int OpenHelp(LPTSTR arg1)
{
HWND _Hhdl = HtmlHelp(GetDesktopWindow(), arg1, HH_DISPLAY_TOPIC, NULL);
return 1;
}
htmlhelp.lib is set in Linker/Input/additional Dependencies.
The error I'm getting is:
main.obj : error LNK2019: unresolved external symbol _HtmlHelpW#16 referenced in function "int __cdecl OpenHelp(wchar_t *)" (?OpenHelp##YAHPA_W#Z)
I'm new to C++ so I'm assuming my function is at fault somehow.
The idea was the function would be passed a sting in the "C:\Help.chm\::/Topic.html" format.
I encounter several errors when calling my MATLAB function from C++. The main idea is: firstly compile a MATLAB function and generate DLL file, and then include .h and .lib files in C++. Finally, write .cpp to test and call the function. Here's my detailed steps and please tell me where I'm wrong.
(Using MATLAB 2012b and Visual C++ 2008, Windows 7 64-bit)
In MATLAB:
mbuild -setup and mex -setup to set Visual Microsoft Visual C++ 2008 SP1 as the compiler.
Create MyAdd.m in folder C:\Users\WangYudong\Documents\MATLAB\MyAdd_M and the function is like:
function [c] = MyAdd(a, b)
c = a + b;
mcc -W cpplib:libMyAdd -T link:lib MyAdd to compile MyAdd.m and generate several files including libMyAdd.dll, libMyAdd.h, libMyAdd.lib and other files.
In C++
Select VC++ Directories → Include files to add E:\MATLAB\R2012b\extern\include.
Select VC++ Directories → Library files to add
E:\MATLAB\R2012b\extern\lib\win64\microsoft and
C:\Users\WangYudong\Documents\MATLAB\MyAdd_M.
Select Linker → Input → Additional Dependencies to add new entries:
mclmcr.lib
mclmcrrt.lib
libmx.lib
libmat.lib
libMyAdd.lib
Create a new MyAdd_test.cpp and put libMyAdd.dll, libMyAdd.h and libMyAdd.lib in the same folder. Add libMyAdd.h in Header Files, libMyAdd.h and libMyAdd.lib in Resource Files.
Code of MyAdd_test.cpp is like:
#include "mclmcr.h"
#include "matrix.h"
#include "mclcppclass.h"
#include "libMyAdd.h"
int main() {
double a = 6;
double b = 9;
double c;
// initialize lib
if( !libMyAddInitialize()) {
std::cout << "Could not initialize libMyAdd!" << std::endl;
return -1;
}
// allocate space
mwArray mwA(1, 1, mxDOUBLE_CLASS);
mwArray mwB(1, 1, mxDOUBLE_CLASS);
mwArray mwC(1, 1, mxDOUBLE_CLASS);
// set data
mwA.SetData(&a, 1);
mwB.SetData(&b, 1);
// use function: MyAdd
MyAdd(1, mwC, mwA, mwB);
// get data
c = mwC.Get(1, 1);
printf("c is %f\n", c);
// terminate the lib
libMyAddTerminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
At last, the result is
Compiling...
MyAdd_test.cpp
Linking...
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclTerminateApplication_proxy referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddTerminate referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl MyAdd(int,class mwArray &,class mwArray const &,class mwArray const &)" (__imp_?MyAdd##YAXHAAVmwArray##ABV1#1#Z) referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddInitialize referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclGetMatrix referenced in function "public: __thiscall mwArray::mwArray(unsigned int,unsigned int,enum mxClassID,enum mxComplexity)" (??0mwArray##QAE#IIW4mxClassID##W4mxComplexity###Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppGetLastError referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error#mwException##SAXXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppCreateError referenced in function "public: __thiscall mwException::mwException(void)" (??0mwException##QAE#XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref referenced in function "public: __thiscall mwException::mwException(class mwException const &)" (??0mwException##QAE#ABV0##Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_release referenced in function "public: virtual __thiscall mwException::~mwException(void)" (??1mwException##UAE#XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _error_info_get_message referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what#mwException##UBEPBDXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_getV_int referenced in function "public: class mwArray __cdecl mwArray::GetPromoted(unsigned int,...)" (?GetPromoted#mwArray##QAA?AV1#IZZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_set_numeric_mxDouble referenced in function "public: void __thiscall mwArray::SetData(double *,unsigned int)" (?SetData#mwArray##QAEXPANI#Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_get_numeric_mxDouble referenced in function "public: __thiscall mwArray::operator double(void)const " (??BmwArray##QBENXZ)
C:\Users\WangYudong\Documents\Visual Studio 2008\Projects\MyAdd_C\Debug\MyAdd_C.exe : fatal error LNK1120: 13 unresolved externals
Actually, the work above is my test to call a custom MATLAB function from C++. My following work is to convert a MATLAB program to C++, which contains image processing functions like imread, edge, strel, etc. I've tried MATLAB Coder, but it can't convert MATLAB functions. So I try the method above. Is it an efficient way to convert those functions or should I implement them using OpenCV?
Before you start, make sure you have a supported compiler installed. That would be Visual C++ and possibly Windows SDK if you are using VS Express edition on a 64-bit Windows. Next you need to configure MATLAB by running these steps at least once:
>> mex -setup
>> mbuild -setup
Now given the following simple function:
MyAdd.m
function c = MyAdd(a,b)
c = a + b;
end
We want to build a C++ shared library using the MATLAB Compiler mcc:
>> mcc -N -W cpplib:libMyAdd -T link:lib MyAdd.m -v
This will produce a couple of files including a header file, a DLL, and an import library:
libMyAdd.h
libMyAdd.dll
libMyAdd.lib
Next we create a C++ program to test the above library:
MyAdd_test.cpp
#include "libMyAdd.h"
int main()
{
// initialize MCR and lib
if (!mclInitializeApplication(NULL,0)) {
std::cerr << "could not initialize the application" << std::endl;
return -1;
}
if(!libMyAddInitialize()) {
std::cerr << "Could not initialize the library" << std::endl;
return -1;
}
try {
// create input
double a[] = {1.0, 2.0, 3.0, 4.0};
double b[] = {5.0, 6.0, 7.0, 8.0};
mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL);
mwArray in2(2, 2, mxDOUBLE_CLASS, mxREAL);
in1.SetData(a, 4);
in2.SetData(b, 4);
// call function
mwArray out;
MyAdd(1, out, in1, in2);
// show result
std::cout << "in1 + in2 = " << std::endl;
std::cout << out << std::endl;
double c[4];
out.GetData(c, 4);
for(int i=0; i<4; i++) {
std::cout << c[i] << " " << std::endl;
}
} catch (const mwException& e) {
std::cerr << e.what() << std::endl;
return -2;
} catch (...) {
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
// cleanup
libMyAddTerminate();
mclTerminateApplication();
return 0;
}
We could compile this program right from inside MATLAB using:
>> mbuild MyAdd_test.cpp libMyAdd.lib -v
>> !MyAdd_test
We could also compile it ourselves using Visual Studio. We start by creating a native console application, then set the project settings as follows:
From the menu, select "Project > Properties" and apply the settings to "All configurations" (both debug and release targets)
Under C/C++ properties, set the "Additional Include Directories" by adding both the directory containing the generated header file libMyAdd.h, in addition to the directory containing MATLAB's header files:
$matlabroot\extern\include
Similarly under "Linker", set the "Additional Library Directories". That would be the same directory as before containing libMyAdd.lib, as well as in my case:
$matlabroot\extern\lib\win32\microsoft
Then go to "Linker > Input" and add the following inside "Additional Dependencies":
libMyAdd.lib
mclmcrrt.lib
Finally under "Debugging > Environment", you might want to extend the PATH environment variable to include the directory containing the generated libMyAdd.dll file. That way you can directly hit F5 to compile run the program directly from inside VS. This will be something like:
PATH=%PATH%;C:\path\to\output\folder
If you do this kind of thing often, you could create a property sheet once, which could then be reused in other VC++ projects. See this answer for an example.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
This is the error I have been getting the whole time and I've been trying to figure out how to fix it but have failed. I am asking if anyone can point me to the right direction.
WorldServer fatal error LNK1120: 2 unresolved externals
WorldServer error LNK2019: unresolved external symbol "public: class CItemElem * __thiscall CLinkedItemMgr::GetLinkedItem(unsigned long)" (?GetLinkedItem#CLinkedItemMgr##QAEPAVCItemElem##K#Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
WorldServer error LNK2019: unresolved external symbol "public: int __thiscall CLinkedItemMgr::AddLinkedItem(class CItemElem *)" (?AddLinkedItem#CLinkedItemMgr##QAEHPAVCItemElem###Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
This is the .h
#ifndef __ITEM_LINK__H
#define __ITEM_LINK__H
class CLinkedItemMgr
{
private:
CLinkedItemMgr(){ m_dwLinkedItemCount = 0;};
~CLinkedItemMgr(){};
DWORD m_dwLinkedItemCount;
public:
map<DWORD,CItemElem*> m_mapLinkedItems;
static CLinkedItemMgr *GetInstance()
{
static CLinkedItemMgr instance;
return &instance;
}
int AddLinkedItem(CItemElem *pItem);
CItemElem *GetLinkedItem(DWORD dwIndex);
};
#endif
this is the .cpp
#include "stdafx.h"
#include "ItemLink.h"
int CLinkedItemMgr::AddLinkedItem(CItemElem *pItem)
{
if(!pItem)
return 0;
m_mapLinkedItems.insert(make_pair<DWORD,CItemElem*>(++m_dwLinkedItemCount,pItem));
return m_dwLinkedItemCount;
}
CItemElem *CLinkedItemMgr::GetLinkedItem(DWORD dwIndex)
{
map<DWORD,CItemElem*>::iterator it = m_mapLinkedItems.find(dwIndex);
if(it == m_mapLinkedItems.end())
return FALSE;
return it->second;
}
Your problem is in the cpp here.
#ifdef __ITEM_LINK
#include "ItemLink.h"
#ifdef __ITEM_LINK means "only process the code below if __ITEM_LINK is defined"
And in your case, it is not defined. It only gets defined when "ItemLink.h" is included, and "ItemLink.h" only gets included if it's already defined. You've prevented either from happening first.
Remove the #ifdef line.
It looks like a linking problem.
The compiler knows your class has a function called GetLinkedItem but can't find any definition of that function anywhere. Are you linking properly when compiling your executable?
I bet stopping the compiler before linking doesn't trigger any error.
(e.g. g++ -c ItemLink.cpp).
i'm going to ask the help of someone who's accustomed to visual studio to elaborate more :D
anyway, compiling requires three major steps:
1) applying preprocessor directives, parsing the source code, looking for syntax errors and the like
2) creating an object file from source code (something half-way between source code and executable)
3) linking all the object files making up your project in one executable
your compiling chain fails at the third step.
the compiler expects a certain function to be defined in some .cpp (that has become an object file at step 2 of compiling chain) but can't find it anywhere.
and it can't find it because of that #ifdef in the .cpp file, which tells the preprocessor NOT TO INCLUDE your definitions, since __ITEM_LINK is not defined
i see you changed the .cpp in your question by the way
I am trying to make a simple UDP socket class to do some talking between a c++ program and a java program so I'm trying to make a socket class that handles all the UDP transmission but I can't get the program to compile because I get about 8 lnk2019 errors and I have no idea what they even mean. I mainly work in Java and only use c++ when I have to lol. I have 3 files the socket header and code. Also the Udp Socket code is from rFactor-Nesim so the socket code isn't written by me.
UdpSocket.cpp
#include "UdpSocket.hpp"
#include <stdio.h>
UdpSocket::UdpSocket(const char* host, int port)
: mHost(host), mPort(port)
{
}
UdpSocket::~UdpSocket(void)
{
}
void UdpSocket::Open()
{
if(WSAStartup(MAKEWORD(2, 0), &mWinsockData) != 0)
fprintf(stderr, "WSAStartup() failed");
if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("socket() failed");
memset(&mSocketAddress, 0, sizeof(mSocketAddress));
mSocketAddress.sin_family = AF_INET;
mSocketAddress.sin_addr.s_addr = inet_addr(mHost);
mSocketAddress.sin_port = htons(mPort);
}
void UdpSocket::Close()
{
closesocket(mSocket);
WSACleanup();
}
void UdpSocket::Send(char* str, size_t length)
{
size_t result = sendto(mSocket, str, length, 0,
(struct sockaddr *) &mSocketAddress, sizeof(mSocketAddress));
if(result != length)
perror("sendto() sent incorrect number of bytes");
}
UdpSocket.hpp
#ifndef UDPSOCKET_HPP
#define UDPSOCKET_HPP
#include <WinSock.h>
class UdpSocket
{
public:
UdpSocket(const char* host, int port);
~UdpSocket(void);
void Send(char* str, size_t length);
void Open();
void Close();
private:
const char* mHost;
int mPort;
int mSocket;
struct sockaddr_in mSocketAddress;
WSADATA mWinsockData;
};
#endif // UDPSOCKET_HPP
and the main
#include "Socket/UdpSocket.hpp"
#include <iostream>
int Main(){
UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469);
testSocket->Open();
system("pause");
return 0;
}
Any help would be great. I'm not very strong with c++ but I have done a little bit
Console Ouput:
Error 1 error LNK2019: unresolved external symbol __imp__htons#4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 2 error LNK2019: unresolved external symbol __imp__inet_addr#4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 3 error LNK2019: unresolved external symbol __imp__socket#12 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 4 error LNK2019: unresolved external symbol __imp__WSAStartup#8 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 5 error LNK2019: unresolved external symbol __imp__WSACleanup#0 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 6 error LNK2019: unresolved external symbol __imp__closesocket#4 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 7 error LNK2019: unresolved external symbol __imp__sendto#24 referenced in function "public: void __thiscall UdpSocket::Send(char *,unsigned int)" (?Send#UdpSocket##QAEXPADI#Z) UdpSocket.obj SocketTest
Error 8 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib SocketTest
Error 9 fatal error LNK1120: 8 unresolved externals C:\Users\Brendan\Documents\Visual Studio 2008\Projects\SocketTest\Debug\SocketTest.exe SocketTest
It sounds like you're failing to link against Winsock - Ws2_32.lib
If you're building from the command line, add Ws2_32.lib to your link command line.
If you're building from Visual Studio, look for linker flags/settings inside a project configuration dialog.
when you have code in several implementation files, you need to compile all those implementation files and pass the resulting object code files to the linker, which combines them (and other stuff) into an executable
it's not enough to include the header of a module
c++ doesn't (yet) have any technical module concept, so including headers does not magically compile implementation files or pass object code files to the linker
this is not part of the c++ standard, but it's part of everyday tool usage
the linker is telling you that you failed to provide it with object code for your class member functions the Winsock library
that object code is provided by the library file, in visual c++ typically with filename extension ".lib"
in general, when you get a mysterious error, just look up the error number in the documentation
in visual studio that's as simple as pressing the F1 key