[GCC] Specifying include libraries in source files

This is a discussion on [GCC] Specifying include libraries in source files within the C Programming forums, part of the General Programming Boards category; In MSVC you have the Code: #pragma comment(lib, "library-name") pragma to tell the linker to include that library. Does gcc ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    412

    [GCC] Specifying include libraries in source files

    In MSVC you have the
    Code:
    #pragma comment(lib, "library-name")
    pragma to tell the linker to include that library. Does gcc have anything similar, or is the -l command line switch the only way?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    The "-l" switch is portable while the pragma directive is not, so why bother.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    How is "-l" any more portable? It's just as gcc specific as that pragma is microsoft specific.
    Code:
    C:\src\cpp>cl 1.cpp -ld3d9
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.21003.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    cl : Command line warning D9002 : ignoring unknown option '-ld3d9'
    1.cpp
    Microsoft (R) Incremental Linker Version 10.00.21003.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:1.exe
    1.obj
    1.obj : error LNK2019: unresolved external symbol _Direct3DCreate9@4 referenced
    in function _main
    1.exe : fatal error LNK1120: 1 unresolved externals
    And yes, using directx libraries in a discussion about portability might seem stupid But it was hard finding a standard function that the linker didn't include a library for by default.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    Guess I work only on the ones that have a CLI and the "-l' switch is common to all of 'em.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Okey, if it can't be done then it can't be done. No big deal, I normally compile under cygwin if I need to make sure my code is portable.
    I was just curious if you could do something like
    Code:
    #if defined(MSVC)
    #pragma ...etc
    #elif defined(GCC)
    #includelib ..
    #endif
    but I guess not.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    Quote Originally Posted by _Mike View Post
    Okey, if it can't be done then it can't be done. No big deal, I normally compile under cygwin if I need to make sure my code is portable.
    I was just curious if you could do something like
    Code:
    #if defined(MSVC)
    #pragma ...etc
    #elif defined(GCC)
    #includelib ..
    #endif
    but I guess not.
    Yep! you can with gcc; no clue about MSVC. gcc has a predefined macro __GNUC__ that can be evaluated during the preprocessor phase like
    Code:
    #ifdef __GNUC__
        #<gcc specific pragma>
    #elif <MSVC predefined macro, if any>
        #<MSVC specific pragma>
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intentially create program with large run-time memory
    By dsollen in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2009, 12:07 PM
  2. include files not working?
    By rogster001 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2009, 05:02 AM
  3. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 10:25 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. using a class in multiple source files???
    By Crossbow in forum C++ Programming
    Replies: 9
    Last Post: 06-18-2002, 08:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21