LNK2019 Linking error on Windows, works fine on Linux - c++

I am porting large project from Linux to WinCE. Currently facing problem of linking error LNK2019.
AS I can see from the error it is because of the overloaded static function is not defined, only declaration. E.g:
template<bool taValue>
struct type
{
};
typedef type<true> MTrue;
typedef type<false> MFalse;
static int getData(int i, MTrue) { return 2; }
static int getData(int i, MFalse);
This is just a sample of code and not exact scenario.
The compilation is working well for Linux but it is falling linking time for WinCE. If I comment out the overloaded undefined function in Linux, then I can see same set of linking error in Linux as well.
I am not sure, why this is happening this case?
Is there any compiler option which can avoid this linking error in case of Linux?
Note: compiler gcc/g++ 4.2.2 and cmake version 3.2.2

Related

Compilation error C2048 while compiling in visual studio 2019 MSVC, but works fine with clang++?

I tried to compile the following sample code with clang compiler and it works fine.
Compiler Details: Apple clang version 12.0.0 (clang-1200.0.32.28)
Target: x86_64-apple-darwin20.1.0
#include <iostream>
#include <stdio.h>
int __cdecl printf(const char* format, ...)
{
std::cout<<"My printf function"<<std::endl;
return 0;
}
int main()
{
printf("Standard printf function\n");
return 0;
}
However, when I tried to compile it in visual studio 2019, it gives a compilation error.
error C2084: function 'int printf(const char *const ,...)' already has a body
What is the reason for compilation failure with MSVC?
How can I make it work, what am I missing?
My goal is to implement my version of printf() function, but wants to use other standard function available in stdio.h. How can I achieve that with MSVC?
From your code you are intentionally trying to use your own version of the printf function.
The error C2084 is expected. You are redefining something already defined. It's dangerous, and you be aware of the risks you are taking. It's a piece of a whole library and any UB (undefined Behaviour) may arise. I won't play with that. From my perspective it's g++ that is not reporting an error, but it may be that the g++/libc prototype of printf is slightly different from the one you wrote, or even, the function printf is declared in the header but not defined in there.
If you really want go down that way I strongly suggest you to define your printf in another source file and hide the libc implementation at linking time. That should be allowed (Warning and errors may arise, but every linker has an override for that).

error: reference to ‘int64’ is ambiguous Compiling problem opencv + dlib in QT

I have strange situation here:
I am a working code base in Mac Os X Qt. I tried to compile on ubuntu same code with same libraries (opencv + dlib ) it give a buch of errors ?
drove me crazy all day :( any solution?
/usr/local/include/opencv2/dnn/dnn.hpp:314:17: error: reference to ‘int64’ is ambiguous
virtual int64 getFLOPS(const std::vector<MatShape> &inputs,
^~~~~
In file included from /usr/local/include/opencv2/core/cvdef.h:91:0,
from /usr/local/include/opencv2/core.hpp:52,
from /usr/local/include/opencv2/highgui.hpp:46,
from ../SFT-V2/worker.h:18,
from ../SFT-V2/widget.h:5,
from ../SFT-V2/main.cpp:1:
/usr/local/include/opencv2/core/hal/interface.h:61:20: note: candidates are: typedef int64_t int64
typedef int64_t int64;
typedef int64_t int64;
Most probably your definition of int64 is clashing with another definition of this type.
Sometimes in ubuntu, the compiler does not allow you to typedef the same thing twice even if the C++ standard allows you.
Reordering includes should solve the problem.
I removed first :
using namespace dlib from the cpp file which is alreayd its header not the error is gone.
But why its working on mac (clang) and not working on ubuntu (g++) ? drove me vrazy all the day to find the solution.

Visual Studio express shows error : "internal error occured in compiler"

I created visual studio files for the c++ library g2o using cmake. But when I try to build it, it is failing showing an error at some template definitions.
template<>
inline void axpy(const Eigen::MatrixXd& A, const Eigen::Map<const Eigen::VectorXd>& x, int xoff, Eigen::Map<Eigen::VectorXd>& y, int yoff)
{
y.segment(yoff, A.rows()) += A * x.segment(xoff, A.cols());
}
As I am not an expert in c++, I am not sure what is going wrong in this line of code. I tried commenting this code and building. Then the build fails on the next template definition. Is it an issue with visual studio?
The error shown is
The same code is working perfectly on linux. But I need to run it on my windows machine as debugging is easier and I have only limited access to the linux machine. Any help will be appreciated.
In windows VS environment, you need to change the template specification.
Change the template<> into template< Eigen::MatrixXd > will works

Runtime library for clang on windows

I have built clang with VS9 following manual.
I have compiled code:
void foo()
{
try
{
throw 1;
}
catch(int)
{}
}
to object file. It has external dependencies:
___cxa_allocate_exception
__ZTIi
___cxa_throw
I guess that these comes from runtime library. But which one should I use? Should I build libc++? Is it stable?
PS: I don't want to have any dependency on cygwin/mingw
PPS: but seems like mingw is what I need... is it only viable option?
You need C++ ABI library. libc++ won't help you here. You need either libc++abi or libsupc++ or something similar.

Building Gstreamer 1.0.5 on Windows

I have the projects setup as described in the readme, and have GLib 2.28.8 installed and compiling. When I get to compiling gstreamer, I get thousands of errors that indicate to me that something is wrong with the build setup or with GLib versions. I couldn't find an easy source of other GLib versions for windows (I've tried both the dev and sources version of GLib here: http://www.gtk.org/download/win32.php ).
The first compile error is:
...\build\gstreamer-1.0.5\gst/gstobject.h(170): error C2079: 'lock' uses undefined struct '_GMutex'
In gstobject, lock is defined as:
GMutex lock; /* object LOCK */
Which Visual studio finds as defined in gthread.h
typedef struct _GMutex GMutex;
So everything looks fine to me, but maybe I'm overlooking something. glibconfig.h also defines _GMutex* as GStaticMutex, could that be interfering?
I am using glib.2.28.8, also encountered the same problem. The following two threads (1 and 2) also discuss the related problem.
In my case, just add
struct _GMutex
{
/*< private >*/
gpointer p;
guint i[2];
};
before
struct _GMutex GMutex;
Maybe you should try to update your Glib to 2.32.0
I successfully build GStreamer-1.0.5 both on Linux and Windows Xp, with Glib 2.32.4
See configure.ac
dnl GLib
GLIB_REQ=2.32.0
AG_GST_GLIB_CHECK([$GLIB_REQ])