Forum > Beginners

[SOLVED] Trying to compile C Object get Error: Multiple defined symbol _main

(1/2) > >>

aileron:
Hi I've been on this for a couple days now and can't seem to break through this last error of multiple defined symbol _main.

I'm following the directions from the PDF at:  ftp://ftp.freepascal.org/fpc/docs-pdf/CinFreePascal.pdf

I'm on windows so I think it might have something to do with a PATH problem getting to libraries for C but I'm not to certain of that. I thought the object file should be complete.

I really am stuck and would really appreciate if someone can point me in the right direction. Here is my code in Lazarus:


--- Code: ---program call_external_functions;

{$mode delphi}{$H+}
{$IFDEF Unix}
{$linklib c}
{$ENDIF}
{$L chello.o}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, CTypes
  { you can add units after this };


//procedure PrintHello; cdecl; external;
//procedure PrintHelloS(nme : ctypes.cint32); cdecl; external;

begin
  readln;

end.
--- End code ---

Just a note the chello.o object file resides in the same directory as the pascal file. The chello.c and chello.h are exactly the same as they are in the PDF and compile with no issues. If I add anything to the code to test it, then the errors will increase.

Thanks in advance to anyone that might be able to enlighten me.

marcov:
Did you compile the C file with "gcc -c", so including the "-c" ?

I quickly tested here (ubuntu 14.04LTS, I used gcc -m32 to compile 32-bits binaries to link with my 32-bit FPC) and it works for me.

aileron:
Yes, I'm on windows and used 'gcc -c' and I think because I'm on windows thats what the problem is.

Side note, I should mention that I use MiniGW version of GCC.

For anybody that doesn't know what I'm compiling its this simple program.

header file chello.h

--- Code: ---#ifndef CHELLO_H
#define CHELLO_H
#include <stdio.h>
#include <stdlib.h>
void PrintHello();
void PrintHelloS(int);
#endif
--- End code ---

chello.c

--- Code: ---#include "chello.h"

int main()
{
    void PrintHello(){
        printf("Hello\n");
        return;
    };

    void PrintHelloS(int nme){
        printf("Hello\n");
        printf("%i",nme);
        return;
    };
}
--- End code ---

Leledumbo:

--- Quote ---#include "chello.h"

int main()
{
    void PrintHello(){
        printf("Hello\n");
        return;
    };

    void PrintHelloS(int nme){
        printf("Hello\n");
        printf("%i",nme);
        return;
    };
}

--- End quote ---
Clearly you didn't follow the PDF as is. Both PrintHello and PrintHelloS in the PDF is not defined inside main (it's a GCC extension, not standard C). There's NO main function required, you're compiling an object file only, not a program. _main is defined by FPC as well as program entry point, hence the multiple defined symbol.

ChrisF:
Well, if you look at your C source code and at the source code provided in the pdf sample, you can see that you've added a -duplicate- main proc;

Remove it (i.e. your source code without it):

--- Code: ---#include "chello.h"

    void PrintHello(){
        printf("Hello\n");
        return;
    };

    void PrintHelloS(int nme){
        printf("Hello\n");
        printf("%i",nme);
        return;
    };

--- End code ---

Anyway, you'll also have to add an import library in your Free Pascal program. Something like (still modifying your source code):

--- Code: ---{$IFDEF Unix}
{$linklib c}
{$ELSE}
{$linklib libmsvcrt}
{$ENDIF}

--- End code ---


** Edit ** Duplicate post with Leledumbo.

Navigation

[0] Message Index

[#] Next page

Go to full version