How can I create a target for an _existing_ library?









up vote
0
down vote

favorite












In CMake, we can add_library(mylib file1.cpp file2.cpp) and have a mylib.a in the library path get built. We can also target_include_directories(mylib INTERFACE some/directory), which effects targets depending on mylib.



But what if I have a library to begin with, which I will not be compiling. How can I add a target relating to it? That what I add as a dependency, will cause the .a file to be linked against, and for which I can set target_include_directories() ?



Note: I'm asking about CMake 3.x.










share|improve this question

























    up vote
    0
    down vote

    favorite












    In CMake, we can add_library(mylib file1.cpp file2.cpp) and have a mylib.a in the library path get built. We can also target_include_directories(mylib INTERFACE some/directory), which effects targets depending on mylib.



    But what if I have a library to begin with, which I will not be compiling. How can I add a target relating to it? That what I add as a dependency, will cause the .a file to be linked against, and for which I can set target_include_directories() ?



    Note: I'm asking about CMake 3.x.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In CMake, we can add_library(mylib file1.cpp file2.cpp) and have a mylib.a in the library path get built. We can also target_include_directories(mylib INTERFACE some/directory), which effects targets depending on mylib.



      But what if I have a library to begin with, which I will not be compiling. How can I add a target relating to it? That what I add as a dependency, will cause the .a file to be linked against, and for which I can set target_include_directories() ?



      Note: I'm asking about CMake 3.x.










      share|improve this question













      In CMake, we can add_library(mylib file1.cpp file2.cpp) and have a mylib.a in the library path get built. We can also target_include_directories(mylib INTERFACE some/directory), which effects targets depending on mylib.



      But what if I have a library to begin with, which I will not be compiling. How can I add a target relating to it? That what I add as a dependency, will cause the .a file to be linked against, and for which I can set target_include_directories() ?



      Note: I'm asking about CMake 3.x.







      cmake






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 21:51









      einpoklum

      33.2k26117234




      33.2k26117234






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          CMake provide an alternate signature for libraries that are already compiled:



          add_library(
          mynamespace::mylib
          STATIC # or it could be SHARED
          IMPORTED
          )


          See the official CMake documentation for more details.



          with that you'll be able to add properties to the target doing so



          set_target_properties(
          mynamespace::mylib
          PROPERTIES
          IMPORTED_LOCATION "path/to/libmylib.a"
          )


          Little precision here, if you're using a Windows DLL, you must pass the DLL file's path in IMPORTED_LOCATION and set another property IMPORTED_IMPLIB that points to the .lib file, (not very handy).



          Note that there is also a equivalent properties per configuration (Debug, and Release), that will need another property to be set (IMPORTED_CONFIGURATION), e.g. IMPORTED_LOCATION_DEBUG.



          See also here and here in the documentation.



          To avoid missing include files you can also precise the include directory using INTERFACE_INCLUDE_DIRECTORY property



          set_target_properties(
          mynamespace::mylib
          PROPERTIES
          INTERFACE_INCLUDE_DIRECTORIES "path/to/mylib/include"
          )


          With this, upon link declaration using target_link_libraries(), CMake will read information of the imported target and will add include directories implicitly.



          Official documentation reference.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253607%2fhow-can-i-create-a-target-for-an-existing-library%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            CMake provide an alternate signature for libraries that are already compiled:



            add_library(
            mynamespace::mylib
            STATIC # or it could be SHARED
            IMPORTED
            )


            See the official CMake documentation for more details.



            with that you'll be able to add properties to the target doing so



            set_target_properties(
            mynamespace::mylib
            PROPERTIES
            IMPORTED_LOCATION "path/to/libmylib.a"
            )


            Little precision here, if you're using a Windows DLL, you must pass the DLL file's path in IMPORTED_LOCATION and set another property IMPORTED_IMPLIB that points to the .lib file, (not very handy).



            Note that there is also a equivalent properties per configuration (Debug, and Release), that will need another property to be set (IMPORTED_CONFIGURATION), e.g. IMPORTED_LOCATION_DEBUG.



            See also here and here in the documentation.



            To avoid missing include files you can also precise the include directory using INTERFACE_INCLUDE_DIRECTORY property



            set_target_properties(
            mynamespace::mylib
            PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES "path/to/mylib/include"
            )


            With this, upon link declaration using target_link_libraries(), CMake will read information of the imported target and will add include directories implicitly.



            Official documentation reference.






            share|improve this answer


























              up vote
              1
              down vote













              CMake provide an alternate signature for libraries that are already compiled:



              add_library(
              mynamespace::mylib
              STATIC # or it could be SHARED
              IMPORTED
              )


              See the official CMake documentation for more details.



              with that you'll be able to add properties to the target doing so



              set_target_properties(
              mynamespace::mylib
              PROPERTIES
              IMPORTED_LOCATION "path/to/libmylib.a"
              )


              Little precision here, if you're using a Windows DLL, you must pass the DLL file's path in IMPORTED_LOCATION and set another property IMPORTED_IMPLIB that points to the .lib file, (not very handy).



              Note that there is also a equivalent properties per configuration (Debug, and Release), that will need another property to be set (IMPORTED_CONFIGURATION), e.g. IMPORTED_LOCATION_DEBUG.



              See also here and here in the documentation.



              To avoid missing include files you can also precise the include directory using INTERFACE_INCLUDE_DIRECTORY property



              set_target_properties(
              mynamespace::mylib
              PROPERTIES
              INTERFACE_INCLUDE_DIRECTORIES "path/to/mylib/include"
              )


              With this, upon link declaration using target_link_libraries(), CMake will read information of the imported target and will add include directories implicitly.



              Official documentation reference.






              share|improve this answer
























                up vote
                1
                down vote










                up vote
                1
                down vote









                CMake provide an alternate signature for libraries that are already compiled:



                add_library(
                mynamespace::mylib
                STATIC # or it could be SHARED
                IMPORTED
                )


                See the official CMake documentation for more details.



                with that you'll be able to add properties to the target doing so



                set_target_properties(
                mynamespace::mylib
                PROPERTIES
                IMPORTED_LOCATION "path/to/libmylib.a"
                )


                Little precision here, if you're using a Windows DLL, you must pass the DLL file's path in IMPORTED_LOCATION and set another property IMPORTED_IMPLIB that points to the .lib file, (not very handy).



                Note that there is also a equivalent properties per configuration (Debug, and Release), that will need another property to be set (IMPORTED_CONFIGURATION), e.g. IMPORTED_LOCATION_DEBUG.



                See also here and here in the documentation.



                To avoid missing include files you can also precise the include directory using INTERFACE_INCLUDE_DIRECTORY property



                set_target_properties(
                mynamespace::mylib
                PROPERTIES
                INTERFACE_INCLUDE_DIRECTORIES "path/to/mylib/include"
                )


                With this, upon link declaration using target_link_libraries(), CMake will read information of the imported target and will add include directories implicitly.



                Official documentation reference.






                share|improve this answer














                CMake provide an alternate signature for libraries that are already compiled:



                add_library(
                mynamespace::mylib
                STATIC # or it could be SHARED
                IMPORTED
                )


                See the official CMake documentation for more details.



                with that you'll be able to add properties to the target doing so



                set_target_properties(
                mynamespace::mylib
                PROPERTIES
                IMPORTED_LOCATION "path/to/libmylib.a"
                )


                Little precision here, if you're using a Windows DLL, you must pass the DLL file's path in IMPORTED_LOCATION and set another property IMPORTED_IMPLIB that points to the .lib file, (not very handy).



                Note that there is also a equivalent properties per configuration (Debug, and Release), that will need another property to be set (IMPORTED_CONFIGURATION), e.g. IMPORTED_LOCATION_DEBUG.



                See also here and here in the documentation.



                To avoid missing include files you can also precise the include directory using INTERFACE_INCLUDE_DIRECTORY property



                set_target_properties(
                mynamespace::mylib
                PROPERTIES
                INTERFACE_INCLUDE_DIRECTORIES "path/to/mylib/include"
                )


                With this, upon link declaration using target_link_libraries(), CMake will read information of the imported target and will add include directories implicitly.



                Official documentation reference.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 17:28









                einpoklum

                33.2k26117234




                33.2k26117234










                answered Nov 12 at 16:40









                Noki

                1267




                1267



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253607%2fhow-can-i-create-a-target-for-an-existing-library%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto