Link CL and CC

Discussion in 'C Programming' started by Rick C. Hodgin, Jan 23, 2014.

  1. Is it possible to link object files generated from Microsoft's C compiler and GNU's C Compiler?

    Best regards,
    Rick C. Hodgin
     
    Rick C. Hodgin, Jan 23, 2014
    #1
    1. Advertisements

  2. CL and GCC. :)
     
    Rick C. Hodgin, Jan 23, 2014
    #2
    1. Advertisements

  3. I was able to get it working. I wound up using a combination of GCC's
    g++ and Microsoft's Visual C++ compiler. COFF object file output in
    GCC was the key.

    Here's are the steps for a sample build on a Windows 7 machine 64-bit:

    (1) Install MinGW on Windows with GCC and bases.
    (2) Create the two source files below.
    (3) Launch a command prompt that has the path for visual studio tools.
    (4) Execute these steps:
    (a) C:\>PATH=%PATH%;c:\mingw\bin\
    (b) C:\>g++ -c -gcoff list.cpp -o list.obj
    (c) C:\>cl /c main.cpp
    (d) C:\>link main.obj list.obj /OUT:main.exe
    (e) C:\>main
    !ne
    !wo
    !three


    =========list.cpp========
    #define RW(x) (char []) { x }

    extern "C"
    {

    char* list[] =
    {
    RW("one"),
    RW("two"),
    RW("three")
    };

    }
    ===========END===========


    =========main.cpp========
    #include <stdlib.h>
    #include <stdio.h>

    extern "C"
    {
    extern char* list[];
    }

    int main(int argc, char* argv[])
    {
    for (int i = 0; list; i++)
    {
    list[0] = '!'; // Prove it's read-write
    puts(list); // Show it
    }
    }
    ===========END===========

    g++ and cl working hand-in-hand. :) Gotta love it. :)

    Note: It also works on list.c and main.c using gcc and cl. To do so,
    remove the extern "C" parts.

    Best regards,
    Rick C. Hodgin
     
    Rick C. Hodgin, Jan 23, 2014
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.