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. Question on g++ compiler options

Question on g++ compiler options

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
7 Posts 2 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.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    Hi, Am getting to grips with the g++ compiler... I have my main cpp file containing the main function in /home/alfred/Public/Test/ I have my header file in /home/alfred/Public/Test/header/ I have my src file for the header in /home/alfred/Public/Test/src/ I used the command g++ -c -Wall -I/home/alfred/Public/Test/header/ myclass.cpp to generate my myclass.o object file and then the archiver to create the library ar rc mylib.a myclass.o which was fine but now I am having problems compiling and linking the main.cpp file to get my exe. I am trying to use the command g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out but I am being returned the error /tmp/ccmZLiDt.o: In function `main': main.cpp:(.text+0x59): undefined reference to `myclass::myclass(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' collect2: ld returned 1 exit status meaning that it cannot see the constructor for the class myclass. I thought the -L flag is to tell it where your library is which has the constructor definition? Ummm, am I getting confused with the -l flag? Thanks for any information. :)

    S 1 Reply Last reply
    0
    • M minkowski

      Hi, Am getting to grips with the g++ compiler... I have my main cpp file containing the main function in /home/alfred/Public/Test/ I have my header file in /home/alfred/Public/Test/header/ I have my src file for the header in /home/alfred/Public/Test/src/ I used the command g++ -c -Wall -I/home/alfred/Public/Test/header/ myclass.cpp to generate my myclass.o object file and then the archiver to create the library ar rc mylib.a myclass.o which was fine but now I am having problems compiling and linking the main.cpp file to get my exe. I am trying to use the command g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out but I am being returned the error /tmp/ccmZLiDt.o: In function `main': main.cpp:(.text+0x59): undefined reference to `myclass::myclass(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' collect2: ld returned 1 exit status meaning that it cannot see the constructor for the class myclass. I thought the -L flag is to tell it where your library is which has the constructor definition? Ummm, am I getting confused with the -l flag? Thanks for any information. :)

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      You still need to tell the linker which library to use. You can:

      1. Add mylib.a to the list of inputs (note that a.out is the default output name from the linker):

        g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a

      2. You can rename the library so it starts with 'lib' - e.g. libmylib.a and then use the -l option by removing the 'lib' prefix:

        g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ -lmylib main.cpp -o a.out

      The -L option tells the linker what directory to look in for library files. The -l option tells the linker which library files to use. HTH!

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        You still need to tell the linker which library to use. You can:

        1. Add mylib.a to the list of inputs (note that a.out is the default output name from the linker):

          g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a

        2. You can rename the library so it starts with 'lib' - e.g. libmylib.a and then use the -l option by removing the 'lib' prefix:

          g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ -lmylib main.cpp -o a.out

        The -L option tells the linker what directory to look in for library files. The -l option tells the linker which library files to use. HTH!

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M Offline
        M Offline
        minkowski
        wrote on last edited by
        #3

        Hey ya, Many thanks for your tips, I tried both but unfortunately I got errors.... For the command line

        g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a

        I got the error g++: mylib.a: No such file or directory and for the command line

        g++ -L/home/alfred/Public/Test/src/ -I/home/alfred/Public/Test/header/ main.cpp -lmylib.a -o a.out

        I got /usr/bin/ld: cannot find -lmylib.a collect2: ld returned 1 exit status even though I have libmylib.a (where the -l substitututes the lib part). Ummm, any ideas? Seems it still can't "see" the mylib.a Many thanks!

        S 1 Reply Last reply
        0
        • M minkowski

          Hey ya, Many thanks for your tips, I tried both but unfortunately I got errors.... For the command line

          g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a

          I got the error g++: mylib.a: No such file or directory and for the command line

          g++ -L/home/alfred/Public/Test/src/ -I/home/alfred/Public/Test/header/ main.cpp -lmylib.a -o a.out

          I got /usr/bin/ld: cannot find -lmylib.a collect2: ld returned 1 exit status even though I have libmylib.a (where the -l substitututes the lib part). Ummm, any ideas? Seems it still can't "see" the mylib.a Many thanks!

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          minkowski wrote:

          For the command line g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a I got the error g++: mylib.a: No such file or directory

          Sorry - for that one, you need the path to the lib on the command-line, not just the name

          minkowski wrote:

          for the command line g++ -L/home/alfred/Public/Test/src/ -I/home/alfred/Public/Test/header/ main.cpp -lmylib.a -o a.out I got /usr/bin/ld: cannot find -lmylib.a collect2: ld returned 1 exit status even though I have libmylib.a (where the -l substitututes the lib part).

          Again - sorry, I forgot to say that you don't need the '.a' part either HTH!

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M 1 Reply Last reply
          0
          • S Stuart Dootson

            minkowski wrote:

            For the command line g++ -L/home/alfred/Public/Test/src -I/home/alfred/Public/Test/header/ main.cpp -o a.out mylib.a I got the error g++: mylib.a: No such file or directory

            Sorry - for that one, you need the path to the lib on the command-line, not just the name

            minkowski wrote:

            for the command line g++ -L/home/alfred/Public/Test/src/ -I/home/alfred/Public/Test/header/ main.cpp -lmylib.a -o a.out I got /usr/bin/ld: cannot find -lmylib.a collect2: ld returned 1 exit status even though I have libmylib.a (where the -l substitututes the lib part).

            Again - sorry, I forgot to say that you don't need the '.a' part either HTH!

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M Offline
            M Offline
            minkowski
            wrote on last edited by
            #5

            Hi ya, Thanks for your reply. The folllowing worked.... g++ -L/home/mpotter/Public/Test/src/mylib.a -I/home/mpotter/Public/Test/header/ main.cpp -o a.out /home/mpotter/Public/Test/src/mylib.a as opposed to g++ -L/home/mpotter/Public/Test/src/ -I/home/mpotter/Public/Test/header/ main.cpp -o a.out mylib.a which did not. also g++ -I/home/mpotter/Public/Test/header/ main.cpp -o a.out /home/mpotter/Public/Test/src/mylib.a works, so I am afraid I don't see the point of using the -L flag to give the location? you can just put the path on the end to your library and it will work fine. I got the g++ -L/home/mpotter/Public/Test/src/ -I/home/mpotter/Public/Test/header/ main.cpp -lmylib -o a.out to work fine. This one with the -l flag makes sense as you use the -L to give the location and -l to give the library name. But the one with just the -L flag (the 1st case) does not make sense to me.... Can you pls explain? Thanks !

            S 1 Reply Last reply
            0
            • M minkowski

              Hi ya, Thanks for your reply. The folllowing worked.... g++ -L/home/mpotter/Public/Test/src/mylib.a -I/home/mpotter/Public/Test/header/ main.cpp -o a.out /home/mpotter/Public/Test/src/mylib.a as opposed to g++ -L/home/mpotter/Public/Test/src/ -I/home/mpotter/Public/Test/header/ main.cpp -o a.out mylib.a which did not. also g++ -I/home/mpotter/Public/Test/header/ main.cpp -o a.out /home/mpotter/Public/Test/src/mylib.a works, so I am afraid I don't see the point of using the -L flag to give the location? you can just put the path on the end to your library and it will work fine. I got the g++ -L/home/mpotter/Public/Test/src/ -I/home/mpotter/Public/Test/header/ main.cpp -lmylib -o a.out to work fine. This one with the -l flag makes sense as you use the -L to give the location and -l to give the library name. But the one with just the -L flag (the 1st case) does not make sense to me.... Can you pls explain? Thanks !

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              The -L option is only relevant if you use -l to specify the library name. The g++ documentation[^] says: -Ldir Add directory dir to the list of directories to be searched for -l.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              M 1 Reply Last reply
              0
              • S Stuart Dootson

                The -L option is only relevant if you use -l to specify the library name. The g++ documentation[^] says: -Ldir Add directory dir to the list of directories to be searched for -l.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M Offline
                M Offline
                minkowski
                wrote on last edited by
                #7

                hey ya Thanks for that. I can see now why it didn't work for the initial -L case. I thought the -L and -l were independent but apparently not ! :)

                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