Undefined reference to while linking dll library - c++

I am trying to compile one of the metatrader 4 c++ api libraries in code::blocks.
Here is the main program
`#include "stdio.h"
#include "MT4APITest.h"
int main(void)
{
char err[255];
SunkTCPClient* pCl = MT4_TC_StartClient("127.0.0.1", 4567, err);
MT4APITest test;
test.Run(pCl);
return 0;
}`
In build options -> liker settings, I have included relative path "bin/Debug/MT4_API.lib"
In build options -> Search Directories -> Compiler - >, I have included "C:\home\mt4\". This is the folder where I have kept my mt4_API.dll library. In the same folder, I've placed below files.
`main.cpp
MT4_API.h
MT4_APITest.cpp
MT4_APITest.h
mt43.cbp (for code blocks).`
When I compile this, the compilation command turns out to be
mingw32-g++.exe -o bin\Debug\mt43.exe obj\Debug\main.o obj\Debug\MT4APITest.o bin\Debug\MT4_API.lib
and getting the error
C:/cygwin/home/LENOVO/mt4/mt43/main.cpp:10: undefined reference to MT4_TC_StartClient(char const*, int, char*, wchar_t const*)#16'
obj\Debug\MT4APITest.o: In function ZN10MT4APITest15TestAccountInfoEv':
C:/cygwin/home/LENOVO/mt4/mt43/MT4APITest.cpp:48: undefined reference to MT4_TC_GetAccountInfo(SunkTCPClient*, MT4AccountInfo*)#8'
C:/cygwin/home/LENOVO/mt4/mt43/MT4APITest.cpp:48: undefined reference to MT4_TC_GetErrorDescription(MT4_RET_CODE)#4'
obj\Debug\MT4APITest.o: In function ZN10MT4APITest14TestSymbolInfoEv':
C:/cygwin/home/LENOVO/mt4/mt43/MT4APITest.cpp:57: undefined reference to MT4_TC_GetSymbolListCount(SunkTCPClient*, int*)#8
and the list continues. May I know what I am missing here.

Related

How to include and use any of the stuff in the subdirectories of `C:\msys64\mingw64\include`

In my VSCode environment with working msys2 / mingW64 /gcc compilers i'm now trying to use ncurses / curses.
msys is in the default installpath, so gcc / g++ /gdb are in
C:\msys64\mingw64\bin
As i understand it, the headerfiles being used by directives are the ones in
C:\msys64\mingw64\include.
So in general i think this is a question how to properly include and use any of the stuff in the subdirectories of C:\msys64\mingw64\include
For my case with ncurses, in include directory there are two subdirectories with names ncurses and ncursesw with identical content.
To begin, i try with a very simple `helloCurses.cpp file
#include <ncurses/curses.h>
using namespace std;
int main(int argc, char ** argv)
{
// init screen and sets up screen
initscr();
// print to screen
printw("Hello World");
// refreshes the screen
refresh();
// pause the screen output
getch();
// deallocates memory and ends ncurses
endwin();
return 0;
}
obviously the compiler can find the ncurses/curses.h file.
But it still doesn't compile.
I'm getting the error messages:
Executing task: C/C++: MingW g++.exe build active file
Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.cpp -o D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Mathias\AppData\Local\Temp\cc44BFtA.o: in function `main':
D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:7: undefined reference to `__imp_initscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:10: undefined reference to `__imp_printw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:13: undefined reference to `__imp_refresh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_stdscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_wgetch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:19: undefined reference to `__imp_endwin'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
As far as I understand the linker will not find the ncurses library. This should be libncurses.a or libncurses.dll.
These files are in the directory C:\msys64\mingw64\lib.
In the end i like to compile with VSCode tasks.json, but to begin with, the commandline
gcc -I C:\msys64\mingw64\include\ncurses -L C:\msys64\mingw64\lib -lncurses helloCurses.cpp -o helloCurses.exe
didn't work either and still gives me the same errormessage.
I have been pointed to
Why does the order in which libraries are linked sometimes cause errors in GCC?
I'm sure this will at some point become valuable information, but right now this isn't about the order of a multitude of probably codependent libraris. This is just about how to include a very first additional library not being basic enough to be includd by a simple
#include <stdio.h>, but still basic enough to be in the full toolchain being installed bypacman -S --needed base-devel mingw-w64-x86_64-toolchain

Undefined reference error with new filesystem library and clang++7

I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe.
I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code.
#include <iostream>
#include <string>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[])
{
std::string path = "/home/.../Downloads";
for (const auto & entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
The error messages I am getting are:
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I made sure to include the experimental/filesystem header instead of just filesystem which removed any red squiggles in Clion. I tried to compile from CLion as well as from the command line. The compilation string I used was:
clang++-7 -Wall -std=c++17 main.cpp -o app
Does anyone have a sense of what is wrong here? In the compile error messages I see the reference to std::experimental::filesystem::v1::__cxx11::.. and I am wondering why this does not say cxx17, but I was not sure if that was the cause of the issue. I explicitly indicated c++17 in the compilation string above.
filesystem is still experimental and requires an extra library.
If you are using libstdc++, link with -lstdc++fs (or target_link_libraries(${PROJECT_NAME} stdc++fs)).
For libc++, use -lc++fs (similar for the CMake command).

Build library libtorrent debian and link it to program c++

I always have problems when building libraries and linking them, so I hope someone can give me a hand.
I downloaded libtorrent from here and I've built it like they explain here in the building with autotools section (skipping step 1). The building process was successfull I think, but when I did make check the output was:
============================================================================
Testsuite summary for libtorrent-rasterbar 1.0.5
============================================================================
# TOTAL: 0
# PASS: 0
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
Maybe it should say: total X?
I did a little program where I added #include <libtorrent/session.hpp> and when I compile with g++ file.cpp -o file it says libtorrent/session.hpp: No such file or directory.
Should I add some flags to g++ such as -lpthread for other projects and thinks like that?
Thanks
UPDATE:
When installing without building using sudo apt-get install libtorrent-rasterbar-dev and compiling my main.cpp file I get this error:
g++ main.cpp -o file
In file included from /usr/include/libtorrent/session.hpp:49:0,
from main.cpp:2:
/usr/include/libtorrent/config.hpp:46:2: error: #error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in order for asio's declarations to be correct. If you're linking dynamically against libtorrent, define BOOST_ASIO_DYN_LINK otherwise BOOST_ASIO_SEPARATE_COMPILATION. You can also use pkg-config or boost build, to automatically apply these defines
#error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in \
UPDATE 2:
Modified the main.cpp file to add the the following above the libtorrent #include directives:
#ifndef BOOST_ASIO_DYN_LINK
#define BOOST_ASIO_DYN_LINK
#endif
But then I have this problem:
$ g++ main.cpp
/tmp/ccM2ItFb.o: In function `main':
main.cpp:(.text+0x57): undefined reference to `libtorrent::default_storage_constructor(libtorrent::file_storage const&, libtorrent::file_storage const*, std::string const&, libtorrent::file_pool&, std::vector<unsigned char, std::allocator<unsigned char> > const&)'
main.cpp:(.text+0xb9): undefined reference to `libtorrent::session::~session()'
main.cpp:(.text+0x105): undefined reference to `libtorrent::session::~session()'
/tmp/ccM2ItFb.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x162): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x16e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x17a): undefined reference to `boost::system::system_category()'
main.cpp:(.text+0x192): undefined reference to `boost::asio::error::get_netdb_category()'
main.cpp:(.text+0x19e): undefined reference to `boost::asio::error::get_addrinfo_category()'
main.cpp:(.text+0x1aa): undefined reference to `boost::asio::error::get_misc_category()'
/tmp/ccM2ItFb.o: In function `boost::asio::error::get_system_category()':
main.cpp (.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccM2ItFb.o: In function `libtorrent::session::session(libtorrent::fingerprint const&, int, unsigned int)':
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x3c): undefined reference to `libtorrent::rel_clocktime_pools_nolog_resolvecountries_deprecated_dht_ext_()'
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x75): undefined reference to `libtorrent::session::init(std::pair<int, int>, char const*, libtorrent::fingerprint const&, int, unsigned int)'
/tmp/ccM2ItFb.o: In function `void boost::checked_delete<libtorrent::torrent_info const>(libtorrent::torrent_info const*)':
main.cpp:(.text._ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_[_ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_]+0x1a): undefined reference to `libtorrent::torrent_info::~torrent_info()'
collect2: error: ld returned 1 exit status
Also tried compiling using: g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp with the same result.
UPDATE 3:
I was able to solve the problem. I had to compile using:
g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp and add also the link library -ltorrent-rasterbar
Sounds like you've compiled the library but not actually installed it anywhere. An #include directive looks in /usr/include by default, but the libtorrent headers are somewhere in the directory where you built the library. You'll need to either install the library's files into the system directories, or give the compiler an -I option pointing to the libtorrent build directory. (You'll probably also need an -L option when linking, for the same reason.)

Errors while trying to compile OpenCV project on Dev C++

Im getting this error:
D:\Users\JF150696\AppData\Local\Temp\ccrDYwyp.o Source2.cpp:(.text+0xdf): undefined reference to `cv::imread(std::string const&, int)'
D:\Users\JF150696\AppData\Local\Temp\ccrDYwyp.o Source2.cpp:(.text+0xdec): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
D:\Users\JF150696\AppData\Local\Temp\ccrDYwyp.o Source2.cpp:(.text+0xe41): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
d:\devc\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe D:\Users\JF150696\AppData\Local\Temp\ccrDYwyp.o: bad reloc address 0x20 in section `.text$_ZSt4sqrtf[__ZSt4sqrtf]'
D:\devc\opencv\projekty\test 3 opencv\test 3 opencv\collect2.exe [Error] ld returned 1 exit status
What i did in Dev Options:
I have added this command line to compiler:
-L"C:\opencv\build\x86\vc11\lib" -lopencv_highgui248 -lopencv_core248 -lopencv_imgproc248 -lopencv_calib3d248 -lopencv_video248 -lopencv_features2d248 -lopencv_ml248 -lopencv_highgui248 -lopencv_objdetect248 -lopencv_contrib248 -lopencv_legacy248 -lopencv_flann248
This lane to linker options:
-static-libgcc -lopencv_highgui248 -lopencv_core248 -lopencv_imgproc248 -lopencv_calib3d248 -lopencv_video248 -lopencv_features2d248 -lopencv_ml248 -lopencv_highgui248 -lopencv_objdetect248 -lopencv_contrib248 -lopencv_legacy248 -lopencv_flann248
In directiories i have added:
binaries: D:\devc\opencv\build\x86\vc11\bin
libs: D:\devc\opencv\build\x86\vc11\lib
headers C: D:\devc\opencv\build\include\opencv2 D:\devc\opencv\build\include\opencv D:\devc\opencv\build\include
headers C++: same as above
I have added opencv path D:\devc\opencv to PATH variable
My dev C++ version is: 5.7.0, OpenCV: 2.4.8
Anyone know how to fix that?
EDIT
Same problem using CodeBlock
Did you link Source2 to collect2 properly? Source2.cpp should be compiled into an object file before linking with your main program.
If I am not mistaken, the problems are not related to linking libraries it is because imread and imwrite shouldnt be like you called
imread(string, mat)
But you called
imread(string, int)
So it gives error. Same with other lines.
Build the libraries using CMake and CodeBlocks as given in here till step 3 of codeblock settings.
Then include the following as given in the blog to Devc++ C and C++ Includes tab
C:\OpenCV\my_build\install\include
C:\OpenCV\my_build\install\include\opencv
C:\OpenCV\my_build\install\include\opencv2
since you have included opencv and opencv2, remove /opencv2/..and /opencv ..in the header files.
and add the below line to libraries in Devc++
C:\OpenCV\my_build\install\x64\mingw\lib
also add all the .dll.a files in C:\OpenCV\my_build\install\x64\mingw\lib to Project Options.
Copy all the dll files from opencv\my_build\install\x64\mingw\bin and paste to folder where EXE of your program would be built.

Linking a DLL using xerces gives undefined symbols

I'm creating a shared library/DLL using cygwin which makes use of Xerces. When I call the xercesc functions from the main application everything is fine, but when I try to put some code into the library, then I get undefined symbols for all the static stuff that xerxesc defines.
For example:
std::string fromXMLString(XMLCh *oXMLString)
{
std::string result;
xercesc::DOMImplementation *impl = xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
char *temp = xercesc::XMLString::transcode(oXMLString);
result = temp;
xercesc::XMLString::release(&temp);
return result;
}
Linking:
g++ -shared -Wl,-soname,cygsupport.so -L /usr/local/lib -l xerces-c -o cygsupport.so obj/helper/xml_helper.o
When linking the library, I get:
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::DOMImplementationRegistry::getDOMImplementation(wchar_t const*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::transcode(wchar_t const*, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::release(char**, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::release(wchar_t**, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
...
Finally after several days on looking into this issue I found the solution. It is as simple as stupid.
My original linker commandline looked like this:
g++ -shared -o mylib.so -L/usr/local/lib -lxerces-c objects...
Googling on this problem didn't yield anything usefull, so finally I decided to create a fresh new sample project with eclipse and suddenly it worked. The only difference was in the commandline to the linker. When I applied the same order to my main project it suddenly compiled.
g++ -L/usr/local/lib -shared -o mylib.so objects... -lxerces-c
Note that in the above line, the objects come before the library, and apperently this makes the difference. I thought that the ordering of the libs only applies to the libraries, but apparently also the objects must be ordered appropriately.