Thread: create a static library from C++ and C code

  1. #1
    Registered User
    Join Date
    Jan 2009

    create a static library from C++ and C code

    I got some C code files and I have written some C++ files as its wrapper. Now I'd like to create a library from all these C and C++ files. To generate .o files before archiving them into a .a file, should I gcc those .c files and g++ those .cc files separately, or just use either gcc or g++ for all .c and .cc files? If both gcc and g++ can successfully generate .o files, can these .o files be equally reliable for further use?

    I also notice that if I g++ one of my .c files, I will get "error: invalid conversion from ‘void*’ to ‘LIST_NODE*’" kind of error at somewhere like "bktptr = bktptr->next;" where bktptr is of "LIST_NODE*" type, pointer to a sturcture.
    typedef struct _list_node {
      void *ptr;
      void *next;
    } LIST_NODE;
    Should I therefore change "bktptr = bktptr->next;" to be "bktptr = (LIST_NODE*)bktptr->next;"? But if I gcc that .c file, there will be no error. I was wondering what is the actual difference between "gcc" and "g++", instead of gcc only .c and g++ only .cc, does it matter to mix somehow?

    Thanks in advance!
    Last edited by lehe; 04-06-2009 at 04:59 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    g++ compiles the code as C++, gcc as C. Most of the time, C code will also compile as C++ code, but your example shows one case where it doesn't.

    C allows implicit conversion from void* to any other pointer type, and C++ doesn't.

Popular pages Recent additions subscribe to a feed