Create executable and shared library with one makefile











up vote
4
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




























    up vote
    4
    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


























      up vote
      4
      down vote

      favorite









      up vote
      4
      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















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 at 1:25









      Peter Mortensen

      1,03821016




      1,03821016










      asked Nov 23 at 8:37









      Olupo

      235




      235






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          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
            Nov 23 at 9:49












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            Nov 23 at 9:51










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            Nov 23 at 9:53






          • 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
            Nov 23 at 10:04











          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
          });


          }
          });














          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
          5
          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
            Nov 23 at 9:49












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            Nov 23 at 9:51










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            Nov 23 at 9:53






          • 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
            Nov 23 at 10:04















          up vote
          5
          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
            Nov 23 at 9:49












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            Nov 23 at 9:51










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            Nov 23 at 9:53






          • 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
            Nov 23 at 10:04













          up vote
          5
          down vote



          accepted







          up vote
          5
          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 Nov 23 at 9:39









          PerlDuck

          4,95311230




          4,95311230












          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            Nov 23 at 9:49












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            Nov 23 at 9:51










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            Nov 23 at 9:53






          • 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
            Nov 23 at 10:04


















          • Thank you. The option with the new first target is exactly what I was looking for.
            – Olupo
            Nov 23 at 9:49












          • @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
            – PerlDuck
            Nov 23 at 9:51










          • I've read some other makefiles, but most of them were to advanced for my current skill level.
            – Olupo
            Nov 23 at 9:53






          • 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
            Nov 23 at 10:04
















          Thank you. The option with the new first target is exactly what I was looking for.
          – Olupo
          Nov 23 at 9:49






          Thank you. The option with the new first target is exactly what I was looking for.
          – Olupo
          Nov 23 at 9:49














          @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
          – PerlDuck
          Nov 23 at 9:51




          @Olupo You are welcome. When you study other Makefiles you'll encounter this all target very often.
          – PerlDuck
          Nov 23 at 9:51












          I've read some other makefiles, but most of them were to advanced for my current skill level.
          – Olupo
          Nov 23 at 9:53




          I've read some other makefiles, but most of them were to advanced for my current skill level.
          – Olupo
          Nov 23 at 9:53




          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
          Nov 23 at 10:04




          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
          Nov 23 at 10:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%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

          Plaza Victoria

          Puebla de Zaragoza

          Musa