How to compile and link mixed C and C++ project?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to compile and link mixed C and C++ project?

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    How to compile and link mixed C and C++ project?

    I have mixed C and C++ main function to compile and link with some lagency C source and new C++ classes source.

    I used gcc to compile C codes and g++ to compile C++ codes, then used g++ to link objectives together, they passed through with no error, but when I run the main, the monitoring results didn't show on the screen as expected (the main show some but then hang up somewhere on the main function lines, no endless loop for sure!).

    If I used cc to compile C and CC to compile C++ then CC to link, the compilation for C and C++ all passed through, the link failed due undefined or unreferenced functions (library functions, not mine).

    Does any of you have compiled and linked with mixed C and C++ project on SUN UNIX?
    Thanks, a Lot.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    g++ should be able to handle it. Just run both types of files through g++ and see if that works, as a single project.

  3. #3
    Join Date
    Aug 2005
    Posts
    14
    Unfortunately, the legacy C codes with some old C style could not get passed through with g++ compilation, but they passed through with gcc. Since these legacy C codes are shared and cannot be changed, I have to compile them using gcc and compile C++ code uisng g++, then using g++ to link them together, no problem observed. But in the final run, it seems to have a hang up problem in the result output on the screen. Any clue ?

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    Make sure the code is used correctly (perhaps the link is correct, and you have a bug?)

    If the code is legal C, g++ should handle it (might require some commandline flags). I have no idea why this would not be the case, unless you have illegal C?

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Can you be more specific? What does "old C style could not get passed through with g++" mean? No function prototypes? Functions with no explicit return values? In such cases, I still think it would be a good idea to consider a code overhaul, but if that's not option for look for the K&R compatility switch of g++. There should be something like this. I'm quite certain of that.
    Danny Kalev

  6. #6
    Join Date
    Aug 2005
    Posts
    14
    The old C are legal codes that have been used by cc for long time. when I used g++, some code compilation complained that theere was parse errors, I found that C used some C++ preserved keywords like "bitand" as user-defined function, which conflicted with C++ keyword. I could not rename it beasue it was shared by others. So I only have option to use gcc or cc to compile it. by the way, Danney, what do you mean by K&R compatibility with g++.
    Thanks a lot.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    K&R C is the pre-ANSI dialect of C++ described in Kernighan and Rictchie's first edition (1978) of "The C Programming Language". In this dialect there were no function prototypes, no enum, no const/volatile and so on. Many POSIX compilers have a switch or flag that instructs the compiler to accept source files that use this dialect.
    However, bitand is certainly not K&R C. In fact, I believe that the problem here is that your source files may have the wrong extension, which causes the compiler to assume that they're written in C++ instead of C. Check the extensions, and the compiler flags.
    Danny Kalev

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    Ive never found a fix for old code that uses a keyword, except to replace it or try to fool the compiler with a #define (sometimes this works).

Similar Threads

  1. How to compile ssteqr
    By guille in forum C++
    Replies: 1
    Last Post: 06-02-2005, 12:12 AM
  2. Best way to link to .libs
    By kentcb in forum C++
    Replies: 4
    Last Post: 02-25-2005, 03:59 PM
  3. Replies: 0
    Last Post: 05-15-2002, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center