Thread: Linking objects to your C++ projects

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Linking objects to your C++ projects

    I sometimes program with different kinds of languages besides C/C++. These include VB, Quickbasic, 32bit ASM, 16bit ASM, Pascal, etc.

    I make functions and have a slew of obj files, and I was thinking, hey wouldn't it be nice to be able to link one of these to a C++ program and call the external function from Main() ?

    However, I havent had much success with this sort of thing. I am sure I am doing a few things wrong. Let me give you a very simple example. In this example we have a win32 console app programmed in C++ (I use Microsoft Visual C++ 6.0, and even Borland C++ 5 sometimes, either one is ok.) In the Assembly code file, we have a function called Theproc, which just prints something to the console. (I use Masm32, Nasm, and win32asm, either one is ok)
    First is the asm program with our external function.

    Code:
        .486                                    ; create 32 bit code
        .model flat, stdcall                    ; 32 bit memory model
        option casemap :none                    ; case sensitive
     
        include \masm32\include\windows.inc     ; always first
        include \masm32\macros\macros.asm       ; MASM support macros
    
      ; -----------------------------------------------------------------
      ; include files that have MASM format prototypes for function calls
      ; -----------------------------------------------------------------
        include \masm32\include\masm32.inc
        include \masm32\include\gdi32.inc
        include \masm32\include\user32.inc
        include \masm32\include\kernel32.inc
    
      ; ------------------------------------------------
      ; Library files that have definitions for function
      ; exports and tested reliable prebuilt code.
      ; ------------------------------------------------
        includelib \masm32\lib\masm32.lib
        includelib \masm32\lib\gdi32.lib
        includelib \masm32\lib\user32.lib
        includelib \masm32\lib\kernel32.lib
    
        .code                       ; Tell MASM where the code starts
    
    ; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
    
    start:                          ; The CODE entry point to the program
    Theproc proc                    ; This is our function
    
        print chr$("Hey, this actually works.",13,10)
    
    ;   exit                         we don't need the exit since we will be calling from C++ Main()
    ;                                and that exits fine
        ret                         ; But we -> DO <- need a return.
    Theproc endp
    
    ; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
    
    end start                       ; Tell MASM where the program ends
    Ok in the second program, we have our C++ program that calls the Theproc function

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    // THIS is where i would put the extern "C" Theproc() right??
    // Or would it be extern "C" _Theproc() ?
    
    int main()                     // No command line params
    {
    Theproc();
    return 0;
    }
    So how would I code these to make them linkable to eachother? I would use extern "C" and not __export correct? And what would be the correct way to declare my Theproc function public so main() could call it?

    Any help is appriciated.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Listed below is sample code to call an assembly module from a C program. I haven't done assembly programming in quite a while. So, I'm a little rusty and ther may be a better way to do this. I've used Hutch's Masm32 and Microsoft Visual C.

    Have fun,

    Bob

    Code:
    ; To compile: 
    ;ml /c /Cx /coff theproc.asm 
    
    .486
    .model flat, stdcall
    option casemap :none
        include \masm32\include\windows.inc     ; always first
        include \masm32\macros\macros.asm       ; MASM support macros
    
      ; -----------------------------------------------------------------
      ; include files that have MASM format prototypes for function calls
      ; -----------------------------------------------------------------
        include \masm32\include\masm32.inc
        include \masm32\include\gdi32.inc
        include \masm32\include\user32.inc
        include \masm32\include\kernel32.inc
    
      ; ------------------------------------------------
      ; Library files that have definitions for function
      ; exports and tested reliable prebuilt code.
      ; ------------------------------------------------
        includelib \masm32\lib\masm32.lib
        includelib \masm32\lib\gdi32.lib
        includelib \masm32\lib\user32.lib
        includelib \masm32\lib\kernel32.lib
    
    
    
    .code
        
    ;--------------------------------------------
      Theproc PROC
    
      print chr$("Hey, this actually works.",13,10)
    	
    	
    	ret
    
       Theproc ENDP
    ;----------------------------------------------
    END
    ;----------------------------------------------
    
    
    // Compile as:
    // Cl Test.cpp testproc.obj kernel32.lib
    
    
    #include <windows.h>
    
    extern "C" int __stdcall Theproc();
    
    int main()
    {
    Theproc();
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Addendum...

    I have a typo in the previously listed code

    extern "C" int __stdcall Theproc();

    should be

    extern "C" void __stdcall Theproc();

    Bob

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Works!

    Works like a charm. Thank you very much for your help!

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Quote Originally Posted by BobS0327
    Addendum...

    I have a typo in the previously listed code

    extern "C" int __stdcall Theproc();

    should be

    extern "C" void __stdcall Theproc();

    Bob
    Neat! Might it be more difficult to call a C\C++ function from an ASM program?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The asm module calls a C funtion which in turn passes a string back to the asm module. It is built as follows:

    cl /c getCtext.c
    ml /c /coff /Cp main.asm
    link /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib main.obj getCtext.obj

    Bob

    Code:
    ;Main.asm
    .386
    .model flat, stdcall
    option casemap:none
    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\user32.inc
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    
    GetTheCFunctionText PROTO  C
    
    .data
    TheMsgBoxCaption  db "Test calling C function & receiving data",0
    TextReceivedFromCModule     dd  0 
    
    .code
    start:
    invoke GetTheCFunctionText
    mov   TextReceivedFromCModule , eax
    invoke MessageBox,  NULL, TextReceivedFromCModule, addr TheMsgBoxCaption, MB_OK 
    invoke ExitProcess,NULL
    end start
    Code:
    //GetCText.C
    char *GetTheCFunctionText()
    {
       static char msgtobesent[] = "Hey, we actually received data from the C funtion";
       return msgtobesent;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking: Without Creating Projects
    By teck in forum Windows Programming
    Replies: 4
    Last Post: 11-23-2007, 03:59 PM
  2. Linking with two objects
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 07:42 AM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. Linking objects internally
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 02:34 AM
  5. linking objects
    By doleman19 in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 12:37 PM