Linux programming: Compile code with dependencies












2














I am new to linux programming and learning it from The Linux Programming Interface by Michael Kerrisk.



I have to compile my first program that has dependencies.



Directory structure:



--linux-programs
|--seek_io.c
|--lib
|--tlpi_hdr.h
|--error_functions.h
|--error_functions.c
|--get_num.h
|--ename.c.inc


I want to compile seek_io.c program with dependencies in the lib directory, so that I can see how the program works.



I tried a few things, absolutely clueless on how they work following this stackoverflow answer. I get all sorts of errors as I am an absolute beginner to Linux programming, not to programming, linux OS and C.





Trials:



gcc -I ./lib/ -c ./lib/error_functions.c and then gcc -o seek_io.c ./error_function.o gives error:
/usr/lib/gcc/x86_64-linux-gnu/crt1.o: In function _start:
(.text+0x20): undefined reference to main
collect2: error: ld returned 1 exit status



After this run, on ls I find that my seek_io.c is not listed.





Basically the author of the book says for tlpi_hdr.h file:



This header file includes various other header files used by many of the example programs, defines a Boolean data type, and defines macros for calculating the minimum and maximum of two numeric values. Using this header file allows us to make the example programs a bit shorter.





Link to codes for files mentioned above:



tlpi_hdr.h



error_functions.h



error_functions.c



get_num.h



get_num.c



seek_io.c










share|improve this question













migrated from unix.stackexchange.com 2 days ago


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.











  • 1




    err, what command(s) did you run and what error(s) did you see?
    – thrig
    2 days ago










  • @thrig I updated the question with a trial.
    – HarshvardhanSharma
    2 days ago










  • Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
    – ctrl-alt-delor
    2 days ago










  • @ctrl-alt-delor No, I didn't.
    – HarshvardhanSharma
    2 days ago
















2














I am new to linux programming and learning it from The Linux Programming Interface by Michael Kerrisk.



I have to compile my first program that has dependencies.



Directory structure:



--linux-programs
|--seek_io.c
|--lib
|--tlpi_hdr.h
|--error_functions.h
|--error_functions.c
|--get_num.h
|--ename.c.inc


I want to compile seek_io.c program with dependencies in the lib directory, so that I can see how the program works.



I tried a few things, absolutely clueless on how they work following this stackoverflow answer. I get all sorts of errors as I am an absolute beginner to Linux programming, not to programming, linux OS and C.





Trials:



gcc -I ./lib/ -c ./lib/error_functions.c and then gcc -o seek_io.c ./error_function.o gives error:
/usr/lib/gcc/x86_64-linux-gnu/crt1.o: In function _start:
(.text+0x20): undefined reference to main
collect2: error: ld returned 1 exit status



After this run, on ls I find that my seek_io.c is not listed.





Basically the author of the book says for tlpi_hdr.h file:



This header file includes various other header files used by many of the example programs, defines a Boolean data type, and defines macros for calculating the minimum and maximum of two numeric values. Using this header file allows us to make the example programs a bit shorter.





Link to codes for files mentioned above:



tlpi_hdr.h



error_functions.h



error_functions.c



get_num.h



get_num.c



seek_io.c










share|improve this question













migrated from unix.stackexchange.com 2 days ago


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.











  • 1




    err, what command(s) did you run and what error(s) did you see?
    – thrig
    2 days ago










  • @thrig I updated the question with a trial.
    – HarshvardhanSharma
    2 days ago










  • Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
    – ctrl-alt-delor
    2 days ago










  • @ctrl-alt-delor No, I didn't.
    – HarshvardhanSharma
    2 days ago














2












2








2







I am new to linux programming and learning it from The Linux Programming Interface by Michael Kerrisk.



I have to compile my first program that has dependencies.



Directory structure:



--linux-programs
|--seek_io.c
|--lib
|--tlpi_hdr.h
|--error_functions.h
|--error_functions.c
|--get_num.h
|--ename.c.inc


I want to compile seek_io.c program with dependencies in the lib directory, so that I can see how the program works.



I tried a few things, absolutely clueless on how they work following this stackoverflow answer. I get all sorts of errors as I am an absolute beginner to Linux programming, not to programming, linux OS and C.





Trials:



gcc -I ./lib/ -c ./lib/error_functions.c and then gcc -o seek_io.c ./error_function.o gives error:
/usr/lib/gcc/x86_64-linux-gnu/crt1.o: In function _start:
(.text+0x20): undefined reference to main
collect2: error: ld returned 1 exit status



After this run, on ls I find that my seek_io.c is not listed.





Basically the author of the book says for tlpi_hdr.h file:



This header file includes various other header files used by many of the example programs, defines a Boolean data type, and defines macros for calculating the minimum and maximum of two numeric values. Using this header file allows us to make the example programs a bit shorter.





Link to codes for files mentioned above:



tlpi_hdr.h



error_functions.h



error_functions.c



get_num.h



get_num.c



seek_io.c










share|improve this question













I am new to linux programming and learning it from The Linux Programming Interface by Michael Kerrisk.



I have to compile my first program that has dependencies.



Directory structure:



--linux-programs
|--seek_io.c
|--lib
|--tlpi_hdr.h
|--error_functions.h
|--error_functions.c
|--get_num.h
|--ename.c.inc


I want to compile seek_io.c program with dependencies in the lib directory, so that I can see how the program works.



I tried a few things, absolutely clueless on how they work following this stackoverflow answer. I get all sorts of errors as I am an absolute beginner to Linux programming, not to programming, linux OS and C.





Trials:



gcc -I ./lib/ -c ./lib/error_functions.c and then gcc -o seek_io.c ./error_function.o gives error:
/usr/lib/gcc/x86_64-linux-gnu/crt1.o: In function _start:
(.text+0x20): undefined reference to main
collect2: error: ld returned 1 exit status



After this run, on ls I find that my seek_io.c is not listed.





Basically the author of the book says for tlpi_hdr.h file:



This header file includes various other header files used by many of the example programs, defines a Boolean data type, and defines macros for calculating the minimum and maximum of two numeric values. Using this header file allows us to make the example programs a bit shorter.





Link to codes for files mentioned above:



tlpi_hdr.h



error_functions.h



error_functions.c



get_num.h



get_num.c



seek_io.c







linux linux-kernel gcc c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









HarshvardhanSharmaHarshvardhanSharma

112111




112111




migrated from unix.stackexchange.com 2 days ago


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.






migrated from unix.stackexchange.com 2 days ago


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.










  • 1




    err, what command(s) did you run and what error(s) did you see?
    – thrig
    2 days ago










  • @thrig I updated the question with a trial.
    – HarshvardhanSharma
    2 days ago










  • Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
    – ctrl-alt-delor
    2 days ago










  • @ctrl-alt-delor No, I didn't.
    – HarshvardhanSharma
    2 days ago














  • 1




    err, what command(s) did you run and what error(s) did you see?
    – thrig
    2 days ago










  • @thrig I updated the question with a trial.
    – HarshvardhanSharma
    2 days ago










  • Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
    – ctrl-alt-delor
    2 days ago










  • @ctrl-alt-delor No, I didn't.
    – HarshvardhanSharma
    2 days ago








1




1




err, what command(s) did you run and what error(s) did you see?
– thrig
2 days ago




err, what command(s) did you run and what error(s) did you see?
– thrig
2 days ago












@thrig I updated the question with a trial.
– HarshvardhanSharma
2 days ago




@thrig I updated the question with a trial.
– HarshvardhanSharma
2 days ago












Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
– ctrl-alt-delor
2 days ago




Did you know that you don't have to program in 'C' to program on Gnu/Linux. However if you are writing kernel code, then you should learn to program user-mode 'C' programs. You need to be very good at 'C', to be an effective kernel programmer.
– ctrl-alt-delor
2 days ago












@ctrl-alt-delor No, I didn't.
– HarshvardhanSharma
2 days ago




@ctrl-alt-delor No, I didn't.
– HarshvardhanSharma
2 days ago












1 Answer
1






active

oldest

votes


















2














The problem is with your second gcc command, where you're using the -o file to specify the output file where to store the resulting executable file, but passing it the name of the C source file seek_io.c instead...



gcc -o seek_io.c ./error_function.o


This means link file error_function.o and store the executable in seek_io.c. This fails because there is no main function, which is needed for a standalone executable, so your C source file is not overwritten by the failing link command.



You can fix this easily by passing the -o option a proper output file name, which in the case (of this link command) should be the name of the executable that you want to create, such as seek_io:



gcc -o seek_io seek_io.c ./error_function.o


(But this will fail without a -I ./lib/, since seek_io.c includes tlpi_hdr.h which is in that directory. If you add it to that command, it should work.)



You can also decide to split the compile and link steps in two separate steps (the command above will both compile seek_io.c into an object file and then link the two object files into an executable) with:



$ gcc -I ./lib/ -c ./lib/error_functions.c
$ gcc -I ./lib/ -c seek_io.c
$ gcc -o seek_io seek_io.o error_function.o


One final nitpick is that for the -I flag to specify the directories where to search for the include files, the more common usage has no space between the flag itself and the directory name, so you'll most commonly see -I./lib or even -Ilib.



$ gcc -Ilib -c ./lib/error_functions.c
$ gcc -Ilib -c seek_io.c
$ gcc -o seek_io seek_io.o error_function.o





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',
    autoActivateHeartbeat: false,
    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%2f54064041%2flinux-programming-compile-code-with-dependencies%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









    2














    The problem is with your second gcc command, where you're using the -o file to specify the output file where to store the resulting executable file, but passing it the name of the C source file seek_io.c instead...



    gcc -o seek_io.c ./error_function.o


    This means link file error_function.o and store the executable in seek_io.c. This fails because there is no main function, which is needed for a standalone executable, so your C source file is not overwritten by the failing link command.



    You can fix this easily by passing the -o option a proper output file name, which in the case (of this link command) should be the name of the executable that you want to create, such as seek_io:



    gcc -o seek_io seek_io.c ./error_function.o


    (But this will fail without a -I ./lib/, since seek_io.c includes tlpi_hdr.h which is in that directory. If you add it to that command, it should work.)



    You can also decide to split the compile and link steps in two separate steps (the command above will both compile seek_io.c into an object file and then link the two object files into an executable) with:



    $ gcc -I ./lib/ -c ./lib/error_functions.c
    $ gcc -I ./lib/ -c seek_io.c
    $ gcc -o seek_io seek_io.o error_function.o


    One final nitpick is that for the -I flag to specify the directories where to search for the include files, the more common usage has no space between the flag itself and the directory name, so you'll most commonly see -I./lib or even -Ilib.



    $ gcc -Ilib -c ./lib/error_functions.c
    $ gcc -Ilib -c seek_io.c
    $ gcc -o seek_io seek_io.o error_function.o





    share|improve this answer


























      2














      The problem is with your second gcc command, where you're using the -o file to specify the output file where to store the resulting executable file, but passing it the name of the C source file seek_io.c instead...



      gcc -o seek_io.c ./error_function.o


      This means link file error_function.o and store the executable in seek_io.c. This fails because there is no main function, which is needed for a standalone executable, so your C source file is not overwritten by the failing link command.



      You can fix this easily by passing the -o option a proper output file name, which in the case (of this link command) should be the name of the executable that you want to create, such as seek_io:



      gcc -o seek_io seek_io.c ./error_function.o


      (But this will fail without a -I ./lib/, since seek_io.c includes tlpi_hdr.h which is in that directory. If you add it to that command, it should work.)



      You can also decide to split the compile and link steps in two separate steps (the command above will both compile seek_io.c into an object file and then link the two object files into an executable) with:



      $ gcc -I ./lib/ -c ./lib/error_functions.c
      $ gcc -I ./lib/ -c seek_io.c
      $ gcc -o seek_io seek_io.o error_function.o


      One final nitpick is that for the -I flag to specify the directories where to search for the include files, the more common usage has no space between the flag itself and the directory name, so you'll most commonly see -I./lib or even -Ilib.



      $ gcc -Ilib -c ./lib/error_functions.c
      $ gcc -Ilib -c seek_io.c
      $ gcc -o seek_io seek_io.o error_function.o





      share|improve this answer
























        2












        2








        2






        The problem is with your second gcc command, where you're using the -o file to specify the output file where to store the resulting executable file, but passing it the name of the C source file seek_io.c instead...



        gcc -o seek_io.c ./error_function.o


        This means link file error_function.o and store the executable in seek_io.c. This fails because there is no main function, which is needed for a standalone executable, so your C source file is not overwritten by the failing link command.



        You can fix this easily by passing the -o option a proper output file name, which in the case (of this link command) should be the name of the executable that you want to create, such as seek_io:



        gcc -o seek_io seek_io.c ./error_function.o


        (But this will fail without a -I ./lib/, since seek_io.c includes tlpi_hdr.h which is in that directory. If you add it to that command, it should work.)



        You can also decide to split the compile and link steps in two separate steps (the command above will both compile seek_io.c into an object file and then link the two object files into an executable) with:



        $ gcc -I ./lib/ -c ./lib/error_functions.c
        $ gcc -I ./lib/ -c seek_io.c
        $ gcc -o seek_io seek_io.o error_function.o


        One final nitpick is that for the -I flag to specify the directories where to search for the include files, the more common usage has no space between the flag itself and the directory name, so you'll most commonly see -I./lib or even -Ilib.



        $ gcc -Ilib -c ./lib/error_functions.c
        $ gcc -Ilib -c seek_io.c
        $ gcc -o seek_io seek_io.o error_function.o





        share|improve this answer












        The problem is with your second gcc command, where you're using the -o file to specify the output file where to store the resulting executable file, but passing it the name of the C source file seek_io.c instead...



        gcc -o seek_io.c ./error_function.o


        This means link file error_function.o and store the executable in seek_io.c. This fails because there is no main function, which is needed for a standalone executable, so your C source file is not overwritten by the failing link command.



        You can fix this easily by passing the -o option a proper output file name, which in the case (of this link command) should be the name of the executable that you want to create, such as seek_io:



        gcc -o seek_io seek_io.c ./error_function.o


        (But this will fail without a -I ./lib/, since seek_io.c includes tlpi_hdr.h which is in that directory. If you add it to that command, it should work.)



        You can also decide to split the compile and link steps in two separate steps (the command above will both compile seek_io.c into an object file and then link the two object files into an executable) with:



        $ gcc -I ./lib/ -c ./lib/error_functions.c
        $ gcc -I ./lib/ -c seek_io.c
        $ gcc -o seek_io seek_io.o error_function.o


        One final nitpick is that for the -I flag to specify the directories where to search for the include files, the more common usage has no space between the flag itself and the directory name, so you'll most commonly see -I./lib or even -Ilib.



        $ gcc -Ilib -c ./lib/error_functions.c
        $ gcc -Ilib -c seek_io.c
        $ gcc -o seek_io seek_io.o error_function.o






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        filbrandenfilbranden

        504110




        504110






























            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%2f54064041%2flinux-programming-compile-code-with-dependencies%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

            How to make a Squid Proxy server?

            第一次世界大戦

            Touch on Surface Book