Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. OpenCV in MFC App

OpenCV in MFC App

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studioquestion
23 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Don Guy

    I have double checked to make sure all the path's and lib's setup in the Project Properties are correct. I am not sure whether the size of the final exe is because it's linking all lib's. Like Richard in his response said, maybe the Visual Studio compiler links in all lib's that it deems necessary to run the exe.

    S Offline
    S Offline
    SoMad
    wrote on last edited by
    #7

    Don Guy wrote:

    Like Richard in his response said, maybe the Visual Studio compiler links in all lib's that it deems necessary to run the exe.

    Well, Richard's point is that it only links what it needs, not that it goes searching for more than you are "pointing to". Is it possible for you to comment out all the OpenCV you use and build the application without linking to it? I was just curious to see how large it is without any of the OpenCV. Soren Madsen

    "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

    D 1 Reply Last reply
    0
    • S SoMad

      Don Guy wrote:

      Like Richard in his response said, maybe the Visual Studio compiler links in all lib's that it deems necessary to run the exe.

      Well, Richard's point is that it only links what it needs, not that it goes searching for more than you are "pointing to". Is it possible for you to comment out all the OpenCV you use and build the application without linking to it? I was just curious to see how large it is without any of the OpenCV. Soren Madsen

      "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

      D Offline
      D Offline
      Don Guy
      wrote on last edited by
      #8

      If i remove the Static Link option from the Project Properties and build, then the exe is 100 KB. With the Static Link option in, it's about 6.1 MB. If i leave the Static Option in and just comment out the code, i.e., #include's and other OpenCV references, then am not sure how much it's going to be. I will check that out later. Does that test tells anything?

      S 1 Reply Last reply
      0
      • D Don Guy

        If i remove the Static Link option from the Project Properties and build, then the exe is 100 KB. With the Static Link option in, it's about 6.1 MB. If i leave the Static Option in and just comment out the code, i.e., #include's and other OpenCV references, then am not sure how much it's going to be. I will check that out later. Does that test tells anything?

        S Offline
        S Offline
        SoMad
        wrote on last edited by
        #9

        Yes, it tells me that not only are you statically linking the OpenCV libraries, you are also statically linking MFC, right? I found a post earlier by a developer from the Visual C++ Libraries team. Go through that and see if putting that #define in your stdafx.h can help you out. http://blogs.msdn.com/b/vcblog/archive/2012/02/06/10263387.aspx[^] Soren Madsen

        "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

        D 1 Reply Last reply
        0
        • S SoMad

          Yes, it tells me that not only are you statically linking the OpenCV libraries, you are also statically linking MFC, right? I found a post earlier by a developer from the Visual C++ Libraries team. Go through that and see if putting that #define in your stdafx.h can help you out. http://blogs.msdn.com/b/vcblog/archive/2012/02/06/10263387.aspx[^] Soren Madsen

          "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

          D Offline
          D Offline
          Don Guy
          wrote on last edited by
          #10

          With Static Linking options in, i.e., in Configuration Properties --> General --> Use of MFC = Use MFC in a Static Library, and then comment out all OpenCV code in the app, the total exe size is 4 MB. So basically it reduced 2 MB, but no significant difference. I added the #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS to stdafx.h file and that didn't make any difference. Btw, i am using Visual Studio 2012.

          1 Reply Last reply
          0
          • D Don Guy

            Hello there, I have a C++ app that uses OpenCV. Currently i am statically linking the OpenCV libs into my app. This is adding an extra overhead of 6+ MB. Ideally i would like to remove some of the features that i am not using in OpenCV. The features am using are, Capture frames from Webcam. Face detection. Image formats (JPEG, PNG). Image rotation & resizing. Is it possible to remove other features and trim down the libs? My C++ app is developed in Visual Studio 2012. Thanks in advance.

            S Offline
            S Offline
            SoMad
            wrote on last edited by
            #11

            Based on our back and forth thread above, I believe the real problem is that you are statically linking to the MFC libraries. You don't have to do that to statically link in the OpenCV libraries. I know your intention is to provide a small distributable file, but you end up shooting yourself in the foot. I would link statically to OpenCV, dynamically to MFC and provide two install files. One with the MFC redistributable DLLs and one without. I found a couple of posts on the subject of how much larger programs statically linked with MFC are compared to earlier versions of Visual Studio. Allow me to try out one of CodeProject's features to call in @Joe-Woodbury to this discussion as it looks like he posted one of those questions. http://connect.microsoft.com/VisualStudio/feedback/details/504714/statically-linked-mfc-applications-are-massive[^] http://connect.microsoft.com/VisualStudio/feedback/details/461441/mfc-static-lib-link-exe-size-too-large[^] Soren Madsen

            "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

            J D 2 Replies Last reply
            0
            • S SoMad

              Based on our back and forth thread above, I believe the real problem is that you are statically linking to the MFC libraries. You don't have to do that to statically link in the OpenCV libraries. I know your intention is to provide a small distributable file, but you end up shooting yourself in the foot. I would link statically to OpenCV, dynamically to MFC and provide two install files. One with the MFC redistributable DLLs and one without. I found a couple of posts on the subject of how much larger programs statically linked with MFC are compared to earlier versions of Visual Studio. Allow me to try out one of CodeProject's features to call in @Joe-Woodbury to this discussion as it looks like he posted one of those questions. http://connect.microsoft.com/VisualStudio/feedback/details/504714/statically-linked-mfc-applications-are-massive[^] http://connect.microsoft.com/VisualStudio/feedback/details/461441/mfc-static-lib-link-exe-size-too-large[^] Soren Madsen

              "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #12

              I posted the first of those and the response was pretty much a giant shrug from Microsoft. They did reduce the static link size a little in VS 2012, but they are still big. Unless the program in question has to be small for whatever reason, I'd go the dynamic link route. If it has to be small, you may have to go back to an earlier version of VS 2008. (I never did update that project to VS 2010 because of this.)

              S 1 Reply Last reply
              0
              • J Joe Woodbury

                I posted the first of those and the response was pretty much a giant shrug from Microsoft. They did reduce the static link size a little in VS 2012, but they are still big. Unless the program in question has to be small for whatever reason, I'd go the dynamic link route. If it has to be small, you may have to go back to an earlier version of VS 2008. (I never did update that project to VS 2010 because of this.)

                S Offline
                S Offline
                SoMad
                wrote on last edited by
                #13

                Thanks for responding. Do you know if you can reduce the size by targeting an older version of MFC in the project settings? I am not sure that would be a good idea, but I was wondering if that would make a difference. Soren Madsen

                "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                J 1 Reply Last reply
                0
                • S SoMad

                  Thanks for responding. Do you know if you can reduce the size by targeting an older version of MFC in the project settings? I am not sure that would be a good idea, but I was wondering if that would make a difference. Soren Madsen

                  "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #14

                  The workarounds say you can target an earlier version and it helps. Makes sense since the problem is with too many cross dependencies in MFC.

                  1 Reply Last reply
                  0
                  • S SoMad

                    Based on our back and forth thread above, I believe the real problem is that you are statically linking to the MFC libraries. You don't have to do that to statically link in the OpenCV libraries. I know your intention is to provide a small distributable file, but you end up shooting yourself in the foot. I would link statically to OpenCV, dynamically to MFC and provide two install files. One with the MFC redistributable DLLs and one without. I found a couple of posts on the subject of how much larger programs statically linked with MFC are compared to earlier versions of Visual Studio. Allow me to try out one of CodeProject's features to call in @Joe-Woodbury to this discussion as it looks like he posted one of those questions. http://connect.microsoft.com/VisualStudio/feedback/details/504714/statically-linked-mfc-applications-are-massive[^] http://connect.microsoft.com/VisualStudio/feedback/details/461441/mfc-static-lib-link-exe-size-too-large[^] Soren Madsen

                    "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                    D Offline
                    D Offline
                    Don Guy
                    wrote on last edited by
                    #15

                    How to link statically to OpenCV? For dynamically linking to MFC, do i select, "Use of MFC = Use MFC in a Shared DLL"? Right now for statically linking OpenCV i got, Linker --> General --> Additional Library Directories = C:\opencv\install\x64\vc11\staticlib Linker --> Input --> Additional Dependencies = "List all Lib's" Am i missing something here?

                    S 1 Reply Last reply
                    0
                    • D Don Guy

                      How to link statically to OpenCV? For dynamically linking to MFC, do i select, "Use of MFC = Use MFC in a Shared DLL"? Right now for statically linking OpenCV i got, Linker --> General --> Additional Library Directories = C:\opencv\install\x64\vc11\staticlib Linker --> Input --> Additional Dependencies = "List all Lib's" Am i missing something here?

                      S Offline
                      S Offline
                      SoMad
                      wrote on last edited by
                      #16

                      That is exactly how you do it. By listing the OpenCV lib files under Linker|General|Additional Dependencies, they get linked in statically and you don't need to ship the application with the DLLs. Yes, setting "Use MFC in a Shared DLL" gives you the dynamic linking to MFC I suggest, but the MFC DLLs have to be on your client's machine - there is a very good chance they already are. This gives you that nice 100 KB exe you got here[^]. The fact that you did not mention any linker errors is a very good sign. Soren Madsen

                      "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                      D 1 Reply Last reply
                      0
                      • S SoMad

                        That is exactly how you do it. By listing the OpenCV lib files under Linker|General|Additional Dependencies, they get linked in statically and you don't need to ship the application with the DLLs. Yes, setting "Use MFC in a Shared DLL" gives you the dynamic linking to MFC I suggest, but the MFC DLLs have to be on your client's machine - there is a very good chance they already are. This gives you that nice 100 KB exe you got here[^]. The fact that you did not mention any linker errors is a very good sign. Soren Madsen

                        "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                        D Offline
                        D Offline
                        Don Guy
                        wrote on last edited by
                        #17

                        Well, am getting linker errors now, that too plenty of them. error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' The OpenCV library am trying to link is build as Static Release, but the MFC is now Dynamic. Any ideas?

                        S 1 Reply Last reply
                        0
                        • D Don Guy

                          Well, am getting linker errors now, that too plenty of them. error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' The OpenCV library am trying to link is build as Static Release, but the MFC is now Dynamic. Any ideas?

                          S Offline
                          S Offline
                          SoMad
                          wrote on last edited by
                          #18

                          The /MD /MT options are used for specifying how the Runtime Libraries[^] are linked. Note that this is not the MFC stuff. You should be able to rebuild the OpenCV libraries with the /MD switch instead of /MT. I think that will cause you the least trouble going forward. Soren Madsen

                          "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                          D 1 Reply Last reply
                          0
                          • S SoMad

                            The /MD /MT options are used for specifying how the Runtime Libraries[^] are linked. Note that this is not the MFC stuff. You should be able to rebuild the OpenCV libraries with the /MD switch instead of /MT. I think that will cause you the least trouble going forward. Soren Madsen

                            "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                            D Offline
                            D Offline
                            Don Guy
                            wrote on last edited by
                            #19

                            If i am right, /MD = Dynamic and /MT = Static. If i build OpenCV as /MD and then link to the MFC app, during run time it will ask for OpenCV dll's.

                            S 1 Reply Last reply
                            0
                            • D Don Guy

                              If i am right, /MD = Dynamic and /MT = Static. If i build OpenCV as /MD and then link to the MFC app, during run time it will ask for OpenCV dll's.

                              S Offline
                              S Offline
                              SoMad
                              wrote on last edited by
                              #20

                              I don't know the details of building the OpenCV libraries. I know there is a BUILD_SHARED_LIBS flag, but I don't know if you can set that to OFF (like you have done) and somewhere else specify the /MD switch and have it correctly build all the lib files without a dependency on the DLLs. Soren Madsen

                              "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                              D 1 Reply Last reply
                              0
                              • S SoMad

                                I don't know the details of building the OpenCV libraries. I know there is a BUILD_SHARED_LIBS flag, but I don't know if you can set that to OFF (like you have done) and somewhere else specify the /MD switch and have it correctly build all the lib files without a dependency on the DLLs. Soren Madsen

                                "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                                D Offline
                                D Offline
                                Don Guy
                                wrote on last edited by
                                #21

                                I created a WIN32 console application and statically linked the OpenCV. The final exe is little less than 4 MB. This kinda proves that there's a overhead when using MFC with statically linked lib's. For my purpose this works fine, as the OpenCV will ultimately be running in a WIN32 DLL. Thanks SoMad for your suggestions! Now the next part am working on is adding an XML file to the project and compiling it into the project.

                                S 2 Replies Last reply
                                0
                                • D Don Guy

                                  I created a WIN32 console application and statically linked the OpenCV. The final exe is little less than 4 MB. This kinda proves that there's a overhead when using MFC with statically linked lib's. For my purpose this works fine, as the OpenCV will ultimately be running in a WIN32 DLL. Thanks SoMad for your suggestions! Now the next part am working on is adding an XML file to the project and compiling it into the project.

                                  S Offline
                                  S Offline
                                  SoMad
                                  wrote on last edited by
                                  #22

                                  :thumbsup: It sounds like you are making progress. There is another approach to this and that is how I often deal with these kind of scenarios where I need to include 3rd party libraries (like Live555 and FFmpeg), but I want to reduce the number of distributable files and the interdependency of those files. Since you just mentioned the WIN32 DLL, this might also be what you are planning on doing in the end. Instead of just building the EXE and distributing that, I build the EXE and a DLL for interfacing to the 3rd party library. That way I can tailor the DLL to work with the 3rd party library according to my needs, while compiling the library according to its needs. You should be able to build your DLL with the /MT setting and build your EXE with the /MD and Shared MFC DLL setting then dynamically load the DLL. Soren Madsen

                                  "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                                  1 Reply Last reply
                                  0
                                  • D Don Guy

                                    I created a WIN32 console application and statically linked the OpenCV. The final exe is little less than 4 MB. This kinda proves that there's a overhead when using MFC with statically linked lib's. For my purpose this works fine, as the OpenCV will ultimately be running in a WIN32 DLL. Thanks SoMad for your suggestions! Now the next part am working on is adding an XML file to the project and compiling it into the project.

                                    S Offline
                                    S Offline
                                    SoMad
                                    wrote on last edited by
                                    #23

                                    Don Guy wrote:

                                    Now the next part am working on is adding an XML file to the project and compiling it into the project.

                                    I assume you are putting it in the resource. XResFile - Files Stored in Resources: Part 1 - Text and Binary[^] Soren Madsen

                                    "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups