Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Using private headers/libraries g++

  1. #1
    Join Date
    Jan 2011
    Beans
    38

    [Solved]Using private headers/libraries g++

    I am currently having troubles using private libraries/header files.
    I'm using the stanford libraries for my school.
    There is a .lib file that I placed in /usr/local/lib
    And a few header filed I placed in /usr/local/include/cs106
    and /usr/local/include/cs106/include

    But whenever I compile with g++ -o test sample1.cpp
    I get a error

    sample1.cpp:13:20: fatal error: genlib.h: No such file or directory
    compilation terminated.

    Does that mean it can't find my header file?
    How do I make it detect.
    I've read about dynamic and static libraries, but this is a header file.
    I know header called the library.

    I've seen g++ using the wall argument, but thats to create a library I believe.

    If someone could tell me how to make the g++ compiler auto-detect my headers and libraries that would be great.
    Thanks
    Last edited by Jabrick; November 15th, 2011 at 04:35 AM.

  2. #2
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Using private headers/libraries g++

    Quote Originally Posted by Jabrick View Post
    I am currently having troubles using private libraries/header files.
    I'm using the stanford libraries for my school.
    There is a .lib file that I placed in /usr/local/lib
    And a few header filed I placed in /usr/local/include/cs106
    and /usr/local/include/cs106/include

    But whenever I compile with g++ -o test sample1.cpp
    I get a error

    sample1.cpp:13:20: fatal error: genlib.h: No such file or directory
    compilation terminated.

    Does that mean it can't find my header file?
    How do I make it detect.
    I've read about dynamic and static libraries, but this is a header file.
    I know header called the library.

    I've seen g++ using the wall argument, but thats to create a library I believe.

    If someone could tell me how to make the g++ compiler auto-detect my headers and libraries that would be great.
    Thanks
    You should read about -I<dir> directive of GCC
    Regards,

    Karlson

  3. #3
    Join Date
    Jan 2011
    Beans
    38

    Re: Using private headers/libraries g++

    Quote Originally Posted by karlson View Post
    You should read about -I<dir> directive of GCC
    Ok I believe I got the header right I have done.
    Code:
    g++ -Wall -I /usr/local/include/cs106/ -o test sample1.cpp
    And I get the message

    /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../lib/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    /tmp/ccTBqbpv.o: In function `Main()':
    sample1.cpp.text+0x35): undefined reference to `GetLong()'
    collect2: ld returned 1 exit status


    Then I tried
    Code:
    g++ -Wall -I /usr/local/include/cs106/ -o test sample1.cpp /usr/local/lib/CS106CPPLib.lib -lm -lpthread
    And get same error

    Also tried
    Code:
     g++ -Wall -I /usr/local/include/cs106/ -o test sample1.cpp -l /usr/local/lib/
    And I get
    /usr/bin/ld: cannot find -l/usr/local/lib/
    collect2: ld returned 1 exit status

    I dont understand what I'm doing wrong. I have linked the header and libraries haven't I?

  4. #4
    Join Date
    Oct 2011
    Beans
    30

    Re: Using private headers/libraries g++

    Mind posting your code? It sounds like the first error is because you named your main function Main, with a capital 'm'. Remember C++ is case sensitive and your main function must be called main, otherwise it doesn't know where to start.

    The second undefined reference is (I believe) because the linker can't find your library. Someone more experienced with gcc might tell you a better way to do this, but I think you can specify your library you want to bring in (CS106CPPLib.lib) with the '-l' switch, like this -

    Code:
    g++ -Wall -I /usr/local/include/cs106 -lCSE106CPPLib.lib -o test sample.cpp

  5. #5
    Join Date
    Jan 2011
    Beans
    38

    Re: Using private headers/libraries g++

    Quote Originally Posted by Liiiim View Post
    Mind posting your code? It sounds like the first error is because you named your main function Main, with a capital 'm'. Remember C++ is case sensitive and your main function must be called main, otherwise it doesn't know where to start.

    The second undefined reference is (I believe) because the linker can't find your library. Someone more experienced with gcc might tell you a better way to do this, but I think you can specify your library you want to bring in (CS106CPPLib.lib) with the '-l' switch, like this -

    Code:
    g++ -Wall -I /usr/local/include/cs106 -lCSE106CPPLib.lib -o test sample.cpp
    Ok so I used what you suggested
    And I get

    /usr/bin/ld: cannot find -lCSE106CPPLib.lib
    collect2: ld returned 1 exit status


    So I tried both
    Code:
    export LD_LIBRARY_PATH=/usr/local/lib
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
    And I'm still getting the same error.
    This is the sample c++ I'm trying to compile
    Code:
    /*                                                                                                                                                                                                                                  
     * -----------------------------------------------------                                                                                                                                                                            
     * CS2S03/SE2S03, September 2011                                                                                                                                                                                                    
     * Assignment 1, Question 1                                                                                                                                                                                                         
     * File: PE0106.cpp                                                                                                                                                                                                                 
     * -----------------------------------------------------                                                                                                                                                                            
     * This program reads an integer and then displays the                                                                                                                                                                              
     * number that has the same digits in the reverse order.                                                                                                                                                                            
     * -----------------------------------------------------                                                                                                                                                                            
     */                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
    #include "genlib.h"                                                                                                                                                                                                                 
    #include "simpio.h"                                                                                                                                                                                                                 
    #include <iostream>                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
    /* Private function prototypes */                                                                                                                                                                                                   
                                                                                                                                                                                                                                        
    long DigitReverse(long n);                                                                                                                                                                                                          
                                                                                                                                                                                                                                        
    /* Main program */                                                                                                                                                                                                                  
                                                                                                                                                                                                                                        
    int main() {                                                                                                                                                                                                                        
        long n;                                                                                                                                                                                                                         
                                                                                                                                                                                                                                        
        cout << "This program reverses the digits in an integer." << endl;                                                                                                                                                              
        while (true) {                                                                                                                                                                                                                  
            cout << "Enter a positive integer: ";                                                                                                                                                                                       
            n = GetLong();                                                                                                                                                                                                              
            if (n > 0) break;                                                                                                                                                                                                           
            cout << "Try again." << endl;                                                                                                                                                                                               
        }                                                                                                                                                                                                                               
        cout << "The reversed integer is: " << DigitReverse(n) << endl;                                                                                                                                                                 
                                                                                                                                                                                                                                        
        return 0;                                                                                                                                                                                                                       
    }                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                        
    /*                                                                                                                                                                                                                                  
     * Function: DigitReverse                                                                                                                                                                                                           
     * Usage: rev = DigitReverse(n);                                                                                                                                                                                                    
     * -----------------------------------------------------                                                                                                                                                                            
     * This function returns the integer that has the same                                                                                                                                                                              
     * digits as n but in reverse order                                                                                                                                                                                                 
     */                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
    long DigitReverse(long n) {                                                                                                                                                                                                         
        long reverse = 0;                                                                                                                                                                                                               
                                                                                                                                                                                                                                        
        while (n > 0) {                                                                                                                                                                                                                 
            reverse = reverse * 10 + (n % 10);                                                                                                                                                                                          
            n = n / 10;                                                                                                                                                                                                                 
        }                                                                                                                                                                                                                               
                                                                                                                                                                                                                                        
        return reverse;                                                                                                                                                                                                                 
    }

  6. #6
    Join Date
    Jun 2011
    Beans
    67
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Using private headers/libraries g++

    I think that it may be because GCC automatically looks for a library whose name starts with "lib". I don't know how to override this functionality, so I would recommend renaming or copying the library to libCSE106CPPLib.a and then try relinking.
    EDIT:
    Reading some more GCC documentation has made it clear that it is the -l option specifically that adds the "lib" and ".a" to the file name, so instead, try just specifying the filename regularly.
    Last edited by gardnan; November 9th, 2011 at 12:55 AM.

  7. #7
    Join Date
    Jan 2011
    Beans
    38

    Re: Using private headers/libraries g++

    I don't understand why I can't get this to work.
    Could it be that the library file is not compatible for linux?
    On my school website there are two options to download the stanford libraries. And they are considered "blank-projects".
    There is a version for PC and a version for Mac.
    But I would think its just how the "project" is formatted.
    Since Mac cannot run visual studios express c++?
    I just unzipped the files and moved the headers and the .lib file into my designated location.

    Currently in my .bashrc I have
    Code:
    export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/include/
    export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib/
    And to compile I run what I think is very thorough.
    Code:
    g++ sample1.cpp -o test -I /usr/local/include/cs106/ -L /usr/local/lib/ -l CS106CPPLib
    I renamed the library file to get rid of the .lib extension
    (even though I dont think linux takes ext. into consideration)

    And still get
    Code:
    /usr/bin/ld: cannot find -lCS106CPPLib
    collect2: ld returned 1 exit status
    And I would like to say thank you for the help!
    I am greatful.

  8. #8
    Join Date
    Jun 2011
    Beans
    67
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Using private headers/libraries g++

    The library you downloaded will probably not be compatible with Ubuntu, as you downloaded the Windows version.

    The library needs to be an ELF LSB in order to be used like normal. There may be some obscure GCC feature that allows you to use the code, so long as it does not depend on any Windows libraries. You may be able to get the Mac version to work though. Try downloading those libraries and running the file command on the actual libraries, they should either be ar archives or ELF LSB files if you want to be able to use them.

  9. #9
    Join Date
    Nov 2004
    Beans
    37

    Smile Re: Using private headers/libraries g++

    A couple of quick style notes in case you are interested (aka feel free to ignore me ):

    Code:
    int main()
    IMHO should be
    Code:
    int main(int argc, char* argv[])
    //or
    int main(int argc, char** argv)
    and I would recommend returning EXIT_SUCCESS as defined in stdlib.h rather than 0.

  10. #10
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Using private headers/libraries g++

    Quote Originally Posted by Liiiim View Post
    Code:
    g++ -Wall -I /usr/local/include/cs106 -lCSE106CPPLib.lib -o test sample.cpp
    That won't work. Judging by the extension of the library it's a Windows MSVC++ lib.
    Last edited by karlson; November 9th, 2011 at 04:42 PM.
    Regards,

    Karlson

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •