Question on g++ compiler options
-
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. :)
-
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. :)
You still need to tell the linker which library to use. You can:
-
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
-
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
-
-
You still need to tell the linker which library to use. You can:
-
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
-
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
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!
-
-
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!
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
-
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
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 !
-
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 !
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
-
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