Linker error while compiling code in Windows - c++

I am trying to compile my C++ code in Windows cmd.
I have implemented UnitTest++ in the project. When I run:
g++ main.cpp -IC:\Test\TreeObjModel\include -IC:\Test\unittest-cpp-master\UnitTest++
it gives the following error:
undefined reference to `UnitTest::RunAllTests()' collect2.exe: error:
ld returned 1 exit status
Can anyone help me to resolve this? Is any more info needed?

You probably are missing to compile some other cpp file (UnitTest.cpp?); or maybe you must link to some UnitTest library, where the code for UnitTest::RunAllTests() resides.
The command line option for linking a library with GCC is -l library_name.

Related

How do i call External Code from Oct Files without Errors?

I want to run a Fortran program within Octave. I found a document, that has an example that i followed step by step. Here is the link. I compiled fortransub.f with the command gfortran -c fortransub.f and fortrandemo.cc with the command mkoctfile -c fortrandemo.cc. Now i have two header files. In the document it says :
Both the Fortran and C++ files need to be compiled in order for the example to work.
For me it is really a vague statement. Now to my problem. I don't know if i have to create an octfile. When i try to create one with the command mkoctfile fortrandemo.cc i get the following error:
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\goebl\AppData\Local\Temp/oct-A409yh.o: in function `Ffortrandemo(octav
e_value_list const&, int)':
C:\Users\goebl\Desktop\Test_1/fortrandemo.cc:23: undefined reference to `fortransub_'
collect2.exe: error: ld returned 1 exit status
warning: mkoctfile: building exited with failure status
When i try to run the command that is the next step in the document, i get the following error:
mkoctfile fortrandemo.cc fortransub.f
gfortran: fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files
compilation terminated.
What did i miss or did wrong? Can someone help me out? Thank you very much!

CImg library when compiling return undefined reference to `__imp_SetDIBitsToDevice'

I'm using VSCode on Windows 10 with MinGW compiler installed. I've tried using CImg library to edit images (http://cimg.eu/), and when I'm trying to compile code from tutorial (http://cimg.eu/reference/group__cimg__tutorial.html) I get this error:
C:\Users\Martini\AppData\Local\Temp\ccBswQ5w.o:tutorial.cpp:(.text$_ZN12cimg_library11CImgDisplay5paintEv[_ZN12cimg_library11CImgDisplay5paintEv]+0xba): undefined reference to `__imp_SetDIBitsToDevice'
collect2.exe: error: ld returned 1 exit status
I have CImg.h file located in folder with tutorial.cpp. Here's command I'm using to compile:
g++ tutorial.cpp -o tutorial.exe
It's my first time working with libraries in C++, and CImg looks fairly easy to use, but if you have used other libraries to edit photos, let me know.
Thanks in advance!
You need to link to GDI32 on Windows I think. Something like:
g++ tutorial.cpp -lgdi32 -o tutorial.exe

How to link/include c++ libraries correctly

I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.

Linker errors when using LLVM

I'm trying to use LLVM to build a compiler backend, but I've gotten mired in linker errors. Currently all I attempt to do is include LLVMContext.h (I'm doing the IBM tutorial), but this gives me the following linker error:
$ g++ -o compiler *.o -L/home/jakob/llvm2/lib/*.a -lantlr4-runtime
BayesBaseListener.o:(.data.rel+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'compiler' failed
make: *** [compiler] Error 1
Any idea how to configure LLVM correctly so this doesn't happen?
The option -L is to add a path that the linker uses to search for libraries. The option -l (lower-case L) is to tell the linker to link with a specific library.
For your case though, if you want to link with all static libraries in a specific location, just list the library files as input files:
g++ -o compiler *.o /home/jakob/llvm2/lib/*.a -lantlr4-runtime
Note that I don't use the -L option.

How to compile and link sqlite3 in a C++ program

I get the following response when I try to compile and link sqlite3 in a C++ program.
I use Eclipse Mars for C++
g++ "-LC:\Sqlite\sqlite3.h" -o Stryktips.exe "src\Stryktips.o" "-lC:\Sqlite\sqlite3.h"
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../.
./../x86_64-w64-mingw32/bin/ld.exe:
cannot find -lC:\Sqlite\sqlite3.h
collect2.exe: error: ld returned 1 exit status
C:\Sqlite\sqlite3.h is a header file. And as such you should #include it, not link with it. You should link with a file ending with .lib or .a
And the -L option is to add a directory path, not a file.
So your linker command like should look something like this:
g++ -o Stryktips.exe "src\Stryktips.o" -LC:\Sqlite -lsqlite
That will tell the linker that libraries can be found in the C:\Sqlite directory, and that it should link with the sqlite library.