Recent

Author Topic: [SOLVED] Trying to compile C Object get Error: Multiple defined symbol _main  (Read 6945 times)

aileron

  • New member
  • *
  • Posts: 6
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: [Select]
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.

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.
« Last Edit: August 25, 2015, 07:30:36 am by aileron »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8731
  • FPC developer.
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #1 on: August 22, 2015, 12:52:42 pm »
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.
« Last Edit: August 22, 2015, 12:55:28 pm by marcov »

aileron

  • New member
  • *
  • Posts: 6
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #2 on: August 22, 2015, 06:58:37 pm »
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: [Select]
#ifndef CHELLO_H
#define CHELLO_H
#include <stdio.h>
#include <stdlib.h>
void PrintHello();
void PrintHelloS(int);
#endif

chello.c
Code: [Select]
#include "chello.h"

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

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

Leledumbo

  • Hero Member
  • *****
  • Posts: 8266
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #3 on: August 22, 2015, 07:36:29 pm »
Quote
#include "chello.h"

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

    void PrintHelloS(int nme){
        printf("Hello\n");
        printf("%i",nme);
        return;
    };
}
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

  • Hero Member
  • *****
  • Posts: 542
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #4 on: August 22, 2015, 07:40:45 pm »
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: [Select]
#include "chello.h"

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

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

Anyway, you'll also have to add an import library in your Free Pascal program. Something like (still modifying your source code):
Code: [Select]
{$IFDEF Unix}
{$linklib c}
{$ELSE}
{$linklib libmsvcrt}
{$ENDIF}


** Edit ** Duplicate post with Leledumbo.

aileron

  • New member
  • *
  • Posts: 6
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #5 on: August 24, 2015, 11:03:13 pm »
Thanks everyone for your comments I changed the code and removed the main.  :-[

After fixing that problem I tried the {$linklib libmsvcrt} and it fails to find it.

Quote
Error: Import library not found for libmsvcrt

I don't know how to set the library paths to MINGW; which I figure is the problem. Is there a way to point Lazarus to MINGW's libraries? I've looked all over and can't find it.

ETA: I'm using Lazarus # 1.4.0; FPC version 2.6.4.

On a side note if I leave that libmsvcrt out I get the following error when trying to compile.

Quote
Error: Undefined symbol: _puts
Error: Undefined symbol: _printf

Thanks for the help. :)
« Last Edit: August 25, 2015, 12:05:25 am by aileron »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #6 on: August 25, 2015, 02:00:58 am »
The message for the missing C system functions (i.e. _puts, _printf)  is a consequence of the former error message:
Quote
Error: Import library not found for libmsvcrt

This library (as some others ones) is in the "\lib" subdirectory of your MingGW installation. So, if you've installed MingGW in C:\MinGW, the path for this library is : C:\MinGW\lib

In this case, link your Free Pascal program by adding this path in the search path for Free Pascal:
Code: [Select]
fpc -FuC:\MinGW\lib hello.pas
Adapt the former instruction/path to your own installation.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8266
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #7 on: August 25, 2015, 06:27:54 am »
Code: [Select]
fpc -FuC:\MinGW\lib hello.pas
Correction: -Fl

aileron

  • New member
  • *
  • Posts: 6
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #8 on: August 25, 2015, 07:29:43 am »
Thank you, its working now. :)

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Trying to compile C Object get Error: Multiple defined symbol _main
« Reply #9 on: August 25, 2015, 05:21:57 pm »
Code: [Select]
fpc -FuC:\MinGW\lib hello.pas
Correction: -Fl

You are quite right, though practically both are working in this case.

 

TinyPortal © 2005-2018