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 Offline
    D Offline
    Don Guy
    wrote on last edited by
    #1

    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.

    Richard Andrew x64R S 3 Replies 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.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      As I understand it, the linker only links in code that is actually called, directly or indirectly, so it would seem that your program needs all 6 MB of what's getting linked in. EDIT: Check your linker settings and be sure to exclude unused COMDATs.

      The difficult we do right away... ...the impossible takes slightly longer.

      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
        #3

        I hope you do not consider this an insult, but are you talking about Release or Debug build? 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 hope you do not consider this an insult, but are you talking about Release or Debug build? 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
          #4

          Release

          S 1 Reply Last reply
          0
          • D Don Guy

            Release

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

            Good :) I found your question in the OpenCV forum and see that you are building the static libs yourself (that was going to be my next question). I have not worked with statically linked OpenCV myself, but you listed the libs you added to your project and go on to say that it seems to link all the libs, not just the ones you added. I agree this is most likely your problem. Did you by chance have a path set up under your projects "Library Directories" that you did not remove? 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

              Good :) I found your question in the OpenCV forum and see that you are building the static libs yourself (that was going to be my next question). I have not worked with statically linked OpenCV myself, but you listed the libs you added to your project and go on to say that it seems to link all the libs, not just the ones you added. I agree this is most likely your problem. Did you by chance have a path set up under your projects "Library Directories" that you did not remove? 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
              #6

              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 1 Reply Last reply
              0
              • 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
                                          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