Thread: Need a good guide g++ on how to compile code

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    Need a good guide g++ on how to compile code

    I'm wokring on a project and there are a lot of different libraries that I need to link to (.a files). I am including these header files in my code, and I really have no idea how I'm supposed to do all of this.

    Does anyone know of a good guide (with lots of examples) on how to do this?

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    269
    Any ideas? I just need to know the different phases, etc.

    For example, if I link to a .a file, to I need to include the header in with the -I flag? These things I don't know.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You never include headers with an -I flag; that's not what it's for. Have you read the manual? Do you know what "compiling" and "linking" and "libraries" mean, as words in this context?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by tabstop View Post
    You never include headers with an -I flag; that's not what it's for. Have you read the manual? Do you know what "compiling" and "linking" and "libraries" mean, as words in this context?
    I'm not very familiar with those terms. I guess I can look at the manual, but I was hoping for more of a tutorial. I feel like what I'm doing is pretty basic. I have my own .h and .c files and am building a wrapper around an existing library, that's been compiled as a .a...

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Have you tried typing the words g++ tutorial into a search engine?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by rags_to_riches View Post
    Have you tried typing the words g++ tutorial into a search engine?
    yes, they just give pretty basic tutorials discussing -Wall and stuff. I need something a little more robust.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are only three relevant options you need worry about for additional libraries

    -I for specifying where the pre-processor should look for .h files.
    -L for specifying where the linker should look for .a files.
    -l for specifying which .a files (the linker) should search in.

    As for the names of .h files to #include in the source, and the names of the .a files to pass to the linker, well that's just down to reading the manual for each library you want to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Salem View Post
    There are only three relevant options you need worry about for additional libraries

    -I for specifying where the pre-processor should look for .h files.
    -L for specifying where the linker should look for .a files.
    -l for specifying which .a files (the linker) should search in.

    As for the names of .h files to #include in the source, and the names of the .a files to pass to the linker, well that's just down to reading the manual for each library you want to use.
    ok, so when do I specify the -I, the -L, etc?

    I just need a general tutorial that explains this stuff, the different phases, what they mean, etc. I find it hard that people just automatically know this stuff....

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    269
    for a given file main.cpp, in a directory dir, if all the .h files are also in the directory "dir", do I need to specify those via -I? These things I have no idea. How do I specify the location of my .a file? things like that.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    269
    Ok, so if I'm linking to a .a file, when I do the -I phase, I don't need to specify .a's header files?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You really need to learn how to use a search engine, but I'll spoon feed you I guess: GCC tutorial - An Introduction to GCC

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by rags_to_riches View Post
    You really need to learn how to use a search engine, but I'll spoon feed you I guess: GCC tutorial - An Introduction to GCC
    I saw that. It's $20. I figured there'd be something else out there.

    I've found some, actually, thanks.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    'man gcc'
    Or if you're not on a linux/unix system:
    Directory Options - Using the GNU Compiler Collection (GCC)
    Link Options - Using the GNU Compiler Collection (GCC)

    Quote Originally Posted by dayalsoap View Post
    for a given file main.cpp, in a directory dir, if all the .h files are also in the directory "dir", do I need to specify those via -I?
    No, if all the header files are in the same dir as the source file you don't need -I if you use "file.h" for your #includes instead of <file.h>
    Or if your header files are in 'dir/some_subdir' you can use
    #include "some_subdir/file.h"
    in your source file to avoid needing to use -I

  14. #14
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    As was mentioned, you should read relevant README files, man entries or online help sources for your libraries to see what files and in what order you should link to. If your .a files are in the usual "include" directory, then you generally do: "gcc main.c -lgmp -lcurl" etc. You can also see NetBeans C/C++ build logs - add some libraries trough project options first and see how things are being compiled, when the linking takes place.

    People don't usually know these things automatically, but there are search engines that happen to help, documentations... I find it difficult to believe that you don't quite know how to work with headers and libraries and you get to work on a project with "a lot of different libraries" out of the blue.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by dayalsoap View Post
    Does anyone know of a good guide (with lots of examples) on how to do this?
    Developers usually provide how-to's compile and link their libraries, but in case of C++ it differs from project to project. I cannot imagine how the supposed examples would look like, "here you got 3 .a libraries, using the mentioned GNU linker link them together into an .exe".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free Windows C Compile that give good warning?
    By stahta01 in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 09:20 PM
  2. guide me to make this code better or efficient
    By larne in forum C++ Programming
    Replies: 11
    Last Post: 01-19-2009, 05:54 AM
  3. The good wife's guide :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 09-03-2003, 07:51 PM
  4. the definitive guide to code commenting
    By Aran in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2002, 01:43 PM
  5. good guide on graphs please?
    By ss3x in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 09:19 AM