Create executable and shared library with one makefile











up vote
3
down vote

favorite












Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:



exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared


When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?










share|improve this question









New contributor




Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:



    exe.a: main.c func.c
    gcc main.c func.c -o exe.a
    lib.so: func.c
    gcc func.c -o lib.so -fPIC -shared


    When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?










    share|improve this question









    New contributor




    Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:



      exe.a: main.c func.c
      gcc main.c func.c -o exe.a
      lib.so: func.c
      gcc func.c -o lib.so -fPIC -shared


      When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?










      share|improve this question









      New contributor




      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:



      exe.a: main.c func.c
      gcc main.c func.c -o exe.a
      lib.so: func.c
      gcc func.c -o lib.so -fPIC -shared


      When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?







      compiling make executable shared-library






      share|improve this question









      New contributor




      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      Peter Mortensen

      1,03821016




      1,03821016






      New contributor




      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Olupo

      185




      185




      New contributor




      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Olupo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          By default make will build only the first target in a Makefile
          which is exe.a in your case. You can either tell make to create
          another target:



          make lib.so


          or both targets:



          make lib.so exe.a


          or (preferred) introduce a new first target which is usually named
          all:



          all: exe.a lib.so

          exe.a: main.c func.c
          gcc main.c func.c -o exe.a
          lib.so: func.c
          gcc func.c -o lib.so -fPIC -shared


          Now a simple make will build the (pseudo-) target all which in turn
          depends on exe.a and lib.so, so they get built first. Note that you
          must not have a file called all in your directory in this case because
          then make gets confused.






          share|improve this answer





















          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            2 days ago












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            2 days ago










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            2 days ago






          • 2




            I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
            – PerlDuck
            2 days ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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
          });


          }
          });






          Olupo is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1095317%2fcreate-executable-and-shared-library-with-one-makefile%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
          4
          down vote



          accepted










          By default make will build only the first target in a Makefile
          which is exe.a in your case. You can either tell make to create
          another target:



          make lib.so


          or both targets:



          make lib.so exe.a


          or (preferred) introduce a new first target which is usually named
          all:



          all: exe.a lib.so

          exe.a: main.c func.c
          gcc main.c func.c -o exe.a
          lib.so: func.c
          gcc func.c -o lib.so -fPIC -shared


          Now a simple make will build the (pseudo-) target all which in turn
          depends on exe.a and lib.so, so they get built first. Note that you
          must not have a file called all in your directory in this case because
          then make gets confused.






          share|improve this answer





















          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            2 days ago












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            2 days ago










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            2 days ago






          • 2




            I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
            – PerlDuck
            2 days ago















          up vote
          4
          down vote



          accepted










          By default make will build only the first target in a Makefile
          which is exe.a in your case. You can either tell make to create
          another target:



          make lib.so


          or both targets:



          make lib.so exe.a


          or (preferred) introduce a new first target which is usually named
          all:



          all: exe.a lib.so

          exe.a: main.c func.c
          gcc main.c func.c -o exe.a
          lib.so: func.c
          gcc func.c -o lib.so -fPIC -shared


          Now a simple make will build the (pseudo-) target all which in turn
          depends on exe.a and lib.so, so they get built first. Note that you
          must not have a file called all in your directory in this case because
          then make gets confused.






          share|improve this answer





















          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            2 days ago












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            2 days ago










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            2 days ago






          • 2




            I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
            – PerlDuck
            2 days ago













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          By default make will build only the first target in a Makefile
          which is exe.a in your case. You can either tell make to create
          another target:



          make lib.so


          or both targets:



          make lib.so exe.a


          or (preferred) introduce a new first target which is usually named
          all:



          all: exe.a lib.so

          exe.a: main.c func.c
          gcc main.c func.c -o exe.a
          lib.so: func.c
          gcc func.c -o lib.so -fPIC -shared


          Now a simple make will build the (pseudo-) target all which in turn
          depends on exe.a and lib.so, so they get built first. Note that you
          must not have a file called all in your directory in this case because
          then make gets confused.






          share|improve this answer












          By default make will build only the first target in a Makefile
          which is exe.a in your case. You can either tell make to create
          another target:



          make lib.so


          or both targets:



          make lib.so exe.a


          or (preferred) introduce a new first target which is usually named
          all:



          all: exe.a lib.so

          exe.a: main.c func.c
          gcc main.c func.c -o exe.a
          lib.so: func.c
          gcc func.c -o lib.so -fPIC -shared


          Now a simple make will build the (pseudo-) target all which in turn
          depends on exe.a and lib.so, so they get built first. Note that you
          must not have a file called all in your directory in this case because
          then make gets confused.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          PerlDuck

          4,83111130




          4,83111130












          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            2 days ago












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            2 days ago










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            2 days ago






          • 2




            I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
            – PerlDuck
            2 days ago


















          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            2 days ago












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            2 days ago










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            2 days ago






          • 2




            I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
            – PerlDuck
            2 days ago
















          Thank you. The option with the new first target is exactly what I was looking for.
          – Olupo
          2 days ago






          Thank you. The option with the new first target is exactly what I was looking for.
          – Olupo
          2 days ago














          @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
          – PerlDuck
          2 days ago




          @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
          – PerlDuck
          2 days ago












          I've read some other makefiles, but most of them were to advanced for my current skill level.
          – Olupo
          2 days ago




          I've read some other makefiles, but most of them were to advanced for my current skill level.
          – Olupo
          2 days ago




          2




          2




          I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
          – PerlDuck
          2 days ago




          I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
          – PerlDuck
          2 days ago










          Olupo is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Olupo is a new contributor. Be nice, and check out our Code of Conduct.













          Olupo is a new contributor. Be nice, and check out our Code of Conduct.












          Olupo is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1095317%2fcreate-executable-and-shared-library-with-one-makefile%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







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?