C library in C++ program?

This is a discussion on C library in C++ program? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program for class, and given the task (it's a computer graphics class) C++ is much easier to ...

  1. #1
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61

    C library in C++ program?

    I'm writing a program for class, and given the task (it's a computer graphics class) C++ is much easier to work with. I have everything ported and compiling in C++, but the instructors gave us a library used to create tiff screenshots, and it was compiled with gcc for C programs. Google says that g++ can't link to gcc created libraries, but it would be very annoying to have to use C just for this one tangential routine, which we don't even have to code ourselves. Is there any way to get g++ to link to the library?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Kernel Sanders View Post
    Google says that g++ can't link to gcc created libraries
    Really? I doubt that. Where did you get that?

    http://www.parashift.com/c++-faq-lit...c-and-cpp.html

  3. #3
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by robwhit View Post
    Really? I doubt that. Where did you get that?

    http://www.parashift.com/c++-faq-lit...c-and-cpp.html
    Not mixing C code with C++ code, using a gcc compiled C library in a g++ compiled C++ program

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    //C++
    extern "C" {
    #include <c_header.h>
    }
    
    #include <iostream>
    
    int main()
    {
       std::cout << "I'm in C++\n" ;
       c_function ();
    }
    g++ file.cpp -lc_lib

    That doesn't work?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Kernel Sanders View Post
    Not mixing C code with C++ code, using a gcc compiled C library in a g++ compiled C++ program
    What's the difference? (What is a C library if not a big pile of C code?)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In fact, g++ automatically links to the standard glibc (or whatever C library the compiler normally links to, e.g. the Windows Standard library). This is done so that you can use C functions such as strcpy, rand, printf etc.

    Yes, it very much CAN do that. You need to tell the compiler that the code you are linking to is C rather than C++ with extern "C" ..., but aside from that, it's a no-brainer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    I found the problem, and it was a pretty common one. Apparently the programmer (me) is an idiot. It works fine even without the extern C declaration - there was a standard library that the given library needed, and I wasn't including it

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kernel Sanders View Post
    I found the problem, and it was a pretty common one. Apparently the programmer (me) is an idiot. It works fine even without the extern C declaration - there was a standard library that the given library needed, and I wasn't including it
    Yup, that'll do it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Library program - need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-08-2004, 12:09 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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