Eclipse CDT undefined reference (package that was downloaded via MYSYS2) - c++

So I basically only coded in Java at College, this time
I want to start my new Project in C++ and want to keep the Eclipse IDE that I have always used. I need the openCV and Tesseract packages (import). I have googled and researched for quite a time and I seem to be doing it right? but maybe some of you can tell me otherwise.
What I did:
Downloaded Eclipse CDT
Downloaded MYSYS2
Followed this instructions (MinGW Compiler)
Open MSYS2 shell from start menu
Run pacman -Sy pacman to update the package database
Re-open the shell, run pacman -Syu to update the package database and core system packages
Re-open the shell, run pacman -Su to update the rest
(Reference)
For 64 bits, run pacman -S mingw-w64-x86_64-toolchain
Select which package to install, default is all
You may also need make, run pacman -S make
Installed the libraries/tools that i need
OpenCV
pacman -S mingw64/mingw-w64-x86_64-opencv
Tesseract
pacman -S mingw64/mingw-w64-x86_64-tesseract-ocr
Included the MinGW to PATH (system environment variables)->restart computer
Started a new Eclipse Project-> C++ -> choose MinGW GCC as Toolchain
Basic Hello World -> Works fine
Basic OpenCV example -> doesnt work
It seems to Resolve the Inclusions correctly.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
No Errors there.
FullCode:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
What Eclipse says:
16:54:43 **** Incremental Build of configuration Debug for project hello ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\hello.o" "..\\src\\hello.cpp"
g++ -o hello.exe "src\\hello.o"
src\hello.o: In function `main':
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:17: undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:25: undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:26: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:28: undefined reference to `cv::waitKey(int)'
src\hello.o: In function `cv::String::String(char const*)':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:602: undefined reference to `cv::String::allocate(unsigned long long)'
src\hello.o: In function `cv::String::~String()':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:648: undefined reference to `cv::String::deallocate()'
src\hello.o: In function `cv::Mat::~Mat()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:692: undefined reference to `cv::fastFree(void*)'
src\hello.o: In function `cv::Mat::release()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:804: undefined reference to `cv::Mat::deallocate()'
src\hello.o: In function `cv::Mat::operator=(cv::Mat&&)':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:1371: undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
16:54:46 Build Finished (took 2s.908ms)
It can't find the librarys????
Whats the point of downloading it through MSYS2 if it doesnt connect the library like it does with iostream
Do I need to add all the library objects to the linker settings C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries

Okay so I guess that its obv but for those who have the same mistake, MSYS2 just added all opencv and tesseract files.
If you want to use them you need to specify it to the Linker.
filename: libopencv_core.dll.a
you need to exclude the lib at the beginning and the .dll and .a
linker: opencv_core
all libs can be found on the mingw path: (C:\msys64\mingw64\lib)
in the end you link it with -lopencv_core
or through the Eclipse GUI C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries -> add library -> opencv_core

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 to while linking dll library

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.

How to set corresponding GCC flags and environment variables in Eclipse CDT

Hi i have been trying to make a c++ program of mine to work on Linux GCC
I have used a Text to speech lib called Festival 2.1
<code>
#include<iostream>
#include<stdlib.h>
#include<festival/festival.h>
using namespace std;
int main()
{
int heap_size=210000;
int load_init_files=1;
festival_initialize(load_init_files,heap_size);
festival_say_text("Hi dude, how are you ?");
festival_wait_for_spooler();
return 0;
}
</code>
This is my program.
g++ test.cpp -l Festival -I/usr/include/festival -I/usr/lib/speech_tools/include -leststring -lestools -lestbase
Using this i am able to compile successfully and exec could be created.
But, how do i set env variables etc in Eclipse CDT(ubuntu 11.10) to work using this code ..
Now i am getting the error
/home/gp/WORKSPACE-CDT/LBOT/Debug/../src/test.cpp:21: undefined reference to festival_initialize(int, int)'
/home/gp/WORKSPACE-CDT/LBOT/Debug/../src/test.cpp:22: undefined reference toEST_String::EST_String(char const*)'
/home/gp/WORKSPACE-CDT/LBOT/Debug/../src/test.cpp:22: undefined reference to festival_say_text(EST_String const&)'
/home/gp/WORKSPACE-CDT/LBOT/Debug/../src/test.cpp:23: undefined reference tofestival_wait_for_spooler()'
./src/test.o: In function EST_Chunk::operator--()':
/usr/include/speech_tools/EST_Chunk.h:140: undefined reference toEST_Chunk::~EST_Chunk()'
/usr/include/speech_tools/EST_Chunk.h:140: undefined reference to `EST_Chunk::operator delete(void*)'
collect2: ld returned 1 exit status
make: * [LBOT] Error 1
i found the solution to this.
So if anyone gets the same problem running Festival tts, it can help :)
basically the environment flags and variables can be set in eclipse through project properties and changing the toolchain settings
STEPS:
UBUNTU 11.10
GCC 4.x pre-installed
Elipse CDT was installed using terminal command
pthreads configured in eclipse using: project->preferences->c/c++ build/settings->GCC linker-> library vars add "pthreads"
Festival installed using: sudo apt-get install festival
Festival dev installed using: sudo apt-get install festival-dev festvox-don build-essential g++
Unresolved inclusion error: goto eclipse, project->preferences->c/c++ build/settings and add festival,speech_tools path to c,c++,linker directories
#include<stdio.h>
#include<festival.h>
int main(int argc,char **argv)
{
int heap_size=210000;
int load_init_files=1;
festival_initialize(load_init_files,heap_size);
festival_say_text("it is lunch time");
festival_wait_for_spooler();
return 0;
}
TERMINAL
sudo g++ main.cpp -l Festival -I/usr/include/festival -I/usr/lib/speech_tools/include -l eststring -l estools -l estbase -o nat.out
ECLIPSE
project->preferences->c/c++ build/settings->GCC linker-> library vars add 'Festival'
project->preferences->c/c++ build/settings->GCC linker-> library vars add 'eststring'
project->preferences->c/c++ build/settings->GCC linker-> library vars add 'estools'
project->preferences->c/c++ build/settings->GCC linker-> library vars add 'estbase'

Setting up SDL2 with Eclipse and MinGW on Windows

I'm trying to create a SDL2 project with Eclipse Kepler and MinGW on Windows.
I already added SDL2 libs in MinGW (.a) in C:\MinGW\lib, SDL2 include in MinGW(C:\MinGW\include\SDL2) and I also added in projects properties -> C/C++ general -> paths and symbols -> librairies the following lines in that order :
mingw32
SDL2main
SDL2
Then I put '-mwindows' in MinGW C++ linker at the end of the line "Command line pattern"
I also added -Dmain=SDL_main for the entry point...
But the compiler gives me error :
main.cpp:7: undefined reference to `SDL_CreateWindow'
this is the code :
#include <SDL2/SDL.h>
int main(int, char**)
{
SDL_Window *pWindow = nullptr;
pFenetre = SDL_CreateWindow("Test SDL 2.0", 0, 0, 320, 240, SDL_WINDOW_SHOWN);
if (!pWindow)
{
return -1;
}
SDL_DestroyWindow(pWindow);
return 0;
}
And this is the build console :
Info: Internal Builder is used for build
g++ "-LC:\\MinGW\\lib" -o Test.exe main.o -lmingw32 -lSDL2main -lSDL2 -mwindows
main.o: In function `SDL_main':
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:7: undefined reference to `SDL_CreateWindow'
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:13: undefined reference to `SDL_DestroyWindow'
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: erreur: ld a retourné 1 code d'état d'exécution
what's wrong ?
Make sure you're using the right version of the library. You can't mix 64-bit import libraries with 32-bit compiler. For the SDL2 library you downloaded(SDL2-devel-2.0.0-mingw.tar.gz) it comes with both 32-bit and 64-bit. i686-w64-mingw32 is 32-bit and x86_64-w64-mingw32 is for 64-bit.

undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.