2

I have a bunch of .O files that I generated from a makefile.u which compiles some .C files. I put all of the .O files into my Xcode project and I receive the below error when it builds:

duplicate symbol '_main' in:
     /Users/.../Objects-normal/arm64/AppDelegate.o
     /Users/.../LIBF2C/main.o
ld: 1 duplicate symbol for architecture arm64

I think I am bit confused because the main.o file is needed for my library, but the error seems to be telling me that I cannot have _main declared twice. So is there a solution for this, am I supposed to rename something to get it to build or any other ideas for how this can get resolved?

For some added context, this is specifically for the f2c libraries. So main.c(->main.o) from libf2c is:

/* STARTUP PROCEDURE FOR UNIX FORTRAN PROGRAMS */

#include "stdio.h"
#include "signal1.h"

#ifndef SIGIOT
#ifdef SIGABRT
#define SIGIOT SIGABRT
#endif
#endif

#ifndef KR_headers
#undef VOID
#include "stdlib.h"
#ifdef __cplusplus
extern "C" {
#endif
#endif

#ifndef VOID
#define VOID void
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef NO__STDC
#define ONEXIT onexit
extern VOID f_exit();
#else
#ifndef KR_headers
extern void f_exit(void);
#ifndef NO_ONEXIT
#define ONEXIT atexit
extern int atexit(void (*)(void));
#endif
#else
#ifndef NO_ONEXIT
#define ONEXIT onexit
extern VOID f_exit();
#endif
#endif
#endif

#ifdef KR_headers
extern VOID f_init(), sig_die();
extern int MAIN__();
#define Int /* int */
#else
extern void f_init(void), sig_die(const char*, int);
extern int MAIN__(void);
#define Int int
#endif

static VOID sigfdie(Sigarg)
{
Use_Sigarg;
sig_die("Floating Exception", 1);
}


static VOID sigidie(Sigarg)
{
Use_Sigarg;
sig_die("IOT Trap", 1);
}

#ifdef SIGQUIT
static VOID sigqdie(Sigarg)
{
Use_Sigarg;
sig_die("Quit signal", 1);
}
#endif


static VOID sigindie(Sigarg)
{
Use_Sigarg;
sig_die("Interrupt", 0);
}

static VOID sigtdie(Sigarg)
{
Use_Sigarg;
sig_die("Killed", 0);
}

#ifdef SIGTRAP
static VOID sigtrdie(Sigarg)
{
Use_Sigarg;
sig_die("Trace trap", 1);
}
#endif


int xargc;
char **xargv;

#ifdef __cplusplus
    }
#endif

 int
#ifdef KR_headers
main(argc, argv) int argc; char **argv;
#else
main(int argc, char **argv)
#endif
{
xargc = argc;
xargv = argv;
signal1(SIGFPE, sigfdie);   /* ignore underflow, enable overflow */
#ifdef SIGIOT
signal1(SIGIOT, sigidie);
#endif
#ifdef SIGTRAP
signal1(SIGTRAP, sigtrdie);
#endif
#ifdef SIGQUIT
if(signal1(SIGQUIT,sigqdie) == SIG_IGN)
    signal1(SIGQUIT, SIG_IGN);
#endif
if(signal1(SIGINT, sigindie) == SIG_IGN)
    signal1(SIGINT, SIG_IGN);
signal1(SIGTERM,sigtdie);

#ifdef pdp11
    ldfps(01200); /* detect overflow as an exception */
#endif

f_init();
#ifndef NO_ONEXIT
ONEXIT(f_exit);
#endif
MAIN__();
#ifdef NO_ONEXIT
f_exit();
#endif
exit(0);    /* exit(0) rather than return(0) to bypass Cray bug */
return 0;   /* For compilers that complain of missing return values; */
        /* others will complain that this is unreachable code. */
}
#ifdef __cplusplus
}
#endif
ez4nick
  • 9,756
  • 12
  • 37
  • 69
  • Please provide a proper [mre]. My best guess: You either defined `main` twice or included the file in which `main` is defined in another file. – user17732522 May 28 '22 at 16:41
  • In how many files have you defined a `main` function? Are you perhaps `#include`ing `.c` files? – Ted Lyngmo May 28 '22 at 16:42
  • My understanding is only `main.c`->`main.o` has a `main` function.. But evidently that must not be true if I am getting this error. I am using `#include` on `.h` files but I don't believe it is for any `.c` files – ez4nick May 28 '22 at 16:54
  • The linker error clearly states which two object modules have `_main` defined. This looks like you're using an framework that already defines a `main`, possibly to even eventually invoke some SDK-mandated entry point that *isn't* `main` that you must provide. – WhozCraig May 28 '22 at 17:02
  • @WhozCraig I think what I am not understanding is, then so with the way the C library I have is structured it is not going to compile unless it is structured in such a way that it does not have a `main` and is just run by calling function(s) defined vs running as something entirely separate with a `main` or am I misunderstanding? – ez4nick May 28 '22 at 17:12
  • 1
    @ez4nick I think you got it. Think about you and a buddy programming. They're going to write a `main`, and *you* are going to write some code that *they* call from *their* main. At least that is what the logistics of what I see indicates. – WhozCraig May 28 '22 at 17:24
  • _Side note:_ This is _very_ old code. Doing `#ifdef` for K&R prototypes hasn't been necessary since the 1990's – Craig Estey May 28 '22 at 18:26
  • @WhozCraig Gotcha that makes sense. I may be at a dead end then as my .C files were created from Fortran files with f2c and so in order to run they need to be compiled with the components of libf2c which is what brought me here to this issue. Appreciate the help though. – ez4nick May 28 '22 at 18:35
  • @CraigEstey Yes this is from f2c code so I'd imagine it is very old. Trying to run some old Fortran code on an Xcode project through converting it to C first. – ez4nick May 28 '22 at 18:38
  • Post processing with the `unifdef` program could be your friend – Craig Estey May 28 '22 at 18:45

0 Answers0