0

Hello fellow community,

I am really new to stackoverflow although I often find great and inspiring answers, when I am really stucked. This one I wasn't able to find anything, so I post it.

Short introduction: I teached myself c++ in 2010 and then changed to VB, C# and Java. I completely missed this whole managed system and was really confused, when things didn't work like they used to.

The problem: My application is using openCV and I already managed to have working builds. At the moment I try to show a picture in a windows forms picturbox and at the same time manipulating it with openCV.

include structure (general):

main.cpp includes main.h

main.h includes openCV string msclr/marshal_cppstd.h form1, form2, form3

the probelm ocurrs in form3.h. Basic structure of form3.h:

#pragma once

cv::Mat srcImg;
HBITMAP get_srcImg()
{
    HBITMAP b = CreateBitmap(srcImg.cols, srcImg.rows, 1, 32, srcImg.data);
    return b;
}

namespace ownProject{//this is all automatically generated by the designer
class form {...}

I get:

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "extern "C" struct HBITMAP__ * cdecl CreateBitmap(int,int,unsigned int,unsigned int,void const *)" (?CreateBitmap@@$$J0YAPEAUHBITMAP@@HHIIPEBX@Z) referenced in function "struct HBITMAP__ * cdecl get_srcImg(void)" (?get_srcImg@@$$FYAPEAUHBITMAP@@XZ) Path_recognition

And I don't know how to solve it. If I delete the createBitmap function, everything is working properly. The problem is I need to convert the picture between the pictureBox and openCV-functions. I had unresolved externals before and fixed it by adjusting the include hierarchy and deleting all cpp-files except main.cpp.

I hope you can help me. Thank you.

Sceen
  • 17
  • 7
  • 4
    `Bitmap^ b;` surely isn't C++. And it's completely unrelated to your issue. And your question is really just a duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/q/12573816/1889329). – IInspectable Sep 06 '16 at 08:28
  • Hi, thank you for your quick answer! I delete the "Bitmap^ b" part. But what is it then? I mean I created a VC++ project and I can't use Bitmap without th "^" managed symbol. – Sceen Sep 06 '16 at 08:58
  • 3
    If you don't know the programming language you are using, how do you expect us to help you? It could be anything, C++/CLI, C++/CX, something else. We don't know. It's somewhat sad that you don't either. – IInspectable Sep 06 '16 at 09:00
  • About your link: I looked through the points, but I can't help myself. I use the createBitmap-function, which is recognized by intellisense. Anyway it throws an error. I dont have to declare it, do I? Maybe I am just too stupid, could you please give me another hint? – Sceen Sep 06 '16 at 09:05
  • @IInspectable Uhm no it couldn't be "anything"... System.Drawing.Bitmap is a .NET class, so it's C++/CLI. And he does know what language he's using, he said it, it's C++. (C++/CLI is essentially the same thing.) – user1610015 Sep 06 '16 at 09:08
  • [Failure to link against appropriate libraries/object files or compile implementation files](http://stackoverflow.com/a/12574400/1889329). – IInspectable Sep 06 '16 at 09:09
  • @user1610015: It could be **any** language extension, that happens to be able to interface with .NET. Or it could be a user-defined namespace in C++/CX. So yes, this really could be anything. And C++/CLI most certainly isn't *"essentially the same thing [as C++]"*. If you think so, you don't know either well enough. – IInspectable Sep 06 '16 at 09:14
  • By your logic, the canonical C++ "hello world" program could be any language that just happens to share some of C++'s syntax. And yes, like it or not, C++ and C++/CLI are essentially the same thing. They share like 99% of the same syntax. They look alike. They are compiled similarly. They share the same **essence**. – user1610015 Sep 06 '16 at 09:27
  • @user1610015: Similarities are completely irrelevant here. This is about properly **tagging** a question, and a question tagged [tag:c++] that is using C++/CLI instead won't be easily discovered, making it less useful. And looking at the syntax of the (now deleted) code snippet, it could really be anything, e.g. C++/CX or C++/CLI. Your proposition, that the referenced class is a .NET class is unfounded, when looking at the code snippet alone. – IInspectable Sep 06 '16 at 10:31
  • I'm comparing two languages, therefore similarities determine how close the two are. And the only way you could think that the Bitmap class isn't from .NET is if you have a tendency to add stuff to the System namespace and are stupid enough to think that everyone else does the same. – user1610015 Sep 06 '16 at 10:57
  • @user1610015: *"if you have a tendency to add stuff to the System namespace"* - Maybe it's just me, but I don't see a global namespace resolution in that code. So what warrants calling it *the* System namespace? Maybe it is just some local namespace that has a striking resemblance to a well-known namespace? Can you tell from the source alone? Stop jumping to conclusions, all the more since this is all completely besides the point: A question requires appropriate tags. It's not for us to guess the programming language. The OP should know. – IInspectable Sep 06 '16 at 11:22
  • So you have a tendency to copy .NET's namespace hierarchy within another namespace? That's even more ridiculous. As for the tags, they were appropriate, and no one had to guess the programming language. – user1610015 Sep 06 '16 at 11:38
  • @user1610015: You're a big fan of jumping to conclusions, eh? I pointed out, that it's **impossible** to tell from the code snippets, what programming language it is (and you haven't been able to refute that statement). Had it been C++/CLI, then the question would have been inappropriately tagged. This should all be obvious, but since you apparently skipped over it, now would be a good time to take the [tour]. – IInspectable Sep 06 '16 at 12:03
  • It's not a conclusion, it's proven fact. (Proof: the code compiles under the C++/CLI compiler.) – user1610015 Sep 06 '16 at 12:32
  • @user1610015: The code also compiles under a C++/CX compiler. So is it C++/CX? You are so not making any sense. And you complete ignore that this isn't about compiling code. It's about tagging questions. Seriously, do take the [tour], and visit the [help]. You dearly are in need of both. – IInspectable Sep 06 '16 at 12:34
  • Point taken, I didn't know the Bitmap class was ComVisible, so the code is both C++/CLI and C++/CX. And my point was never about tags, so thanks but no thanks. – user1610015 Sep 06 '16 at 12:51

1 Answers1

0

You should link to Gdi32.lib

#pragma comment(lib, "Gdi32.lib")

That should solve your problem

Asesh
  • 3,186
  • 2
  • 21
  • 31
  • Thank you that seems to solve my problem. Why is the function automatically recognized, if I still needed to include GDI? I expected to get a different error... I mean its like "I perfectly know what you are trying to tell me, but anyway I will throw an error without any suggestions" – Sceen Sep 06 '16 at 09:19
  • @Sceen: Lack of diligence on your part. There are two parties (3, really) involved. Intellisense, the compiler, and the linker. Both Intellisense and the compiler operate on source code, and are well aware of symbols once declared. The linker works with object code, it requires that symbols are defined. You need to go shopping at [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Sep 06 '16 at 09:22
  • @Sceen: As for the *"I will throw an error without any suggestions"*, consider taking a look at the documentation for [LNK2019](https://msdn.microsoft.com/en-us/library/799kze2z.aspx). I see a whole lot of suggestions right there. – IInspectable Sep 06 '16 at 09:25
  • Hmm okay, thanks for the link. Maybe I really forgot a lot. @IInspectable: Yes, but I was on a really bad track. Previously I solved it by reconstruct my include hierarchy (same error). I thought if intellisense knows the function, it is already included. So, yeah it was the right error, but still confusing. Now I know it better ;) – Sceen Sep 06 '16 at 09:25