English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. C/C++ (https://eab.abime.net/forumdisplay.php?f=118)
-   -   stdlib amiga gcc (https://eab.abime.net/showthread.php?t=105239)

Emufr3ak 03 January 2021 11:21

stdlib amiga gcc
 
Maybe Some c/c++ gurus can help.



I'm trying to port some C++ code to Amiga. I'm using abyss visual studio code plugin for development.


For some basic c++ functions like the new operator stdlib is required. On linking stage I get errors like this:


.../ld.exe: cannot find crtbegin.o: No such file or directory
.../ld.exe: cannot find -lgcc
.../ld.exe: cannot find -lc



This is my Makefile. It's the default Makefile from abyss. I just removed the flag -nostdlib.



# to generate assembler sources: -Wa,-ahl=$@.s

forward-to-backward = $(subst /,\,$1)

subdirs := $(wildcard */)
VPATH = $(subdirs)
cpp_sources := $(wildcard *.cpp)$(wildcard $(addsuffix *.cpp,$(subdirs)))
cpp_objects := $(addprefix obj/,$(patsubst %.cpp,%.o,$(notdir $(cpp_sources))))
c_sources := $(wildcard *.c)$(wildcard $(addsuffix *.c,$(subdirs)))
c_objects := $(addprefix obj/,$(patsubst %.c,%.o,$(notdir $(c_sources))))
objects := $(cpp_objects)$(c_objects)

# https://stackoverflow.com/questions/...kefile/4038459
# http://www.microhowto.info/howto/aut...endencies.html

OUT = a.mingw
CC = m68k-amiga-elf-gcc

CCFLAGS = -g -MP -MMD -m68000 -Ofast -std=gnu++11 -Wall -Wno-unused-function -Wno-volatile-register-var -fomit-frame-pointer -fno-tree-loop-distribution -flto -fwhole-program -fno-exceptions
ASFLAGS = -Wa,-g
LDFLAGS = -Wl,--emit-relocs,-Ttext=0,-Map=$(OUT).map

all: $(OUT).exe

$(OUT).exe: $(OUT).elf
$(info Elf2Hunk $(OUT).exe)
@elf2hunk $(OUT).elf $(OUT).exe

$(OUT).elf: $(objects) obj/gcc8_a_support.o
$(info Linking a.mingw.elf)
@$(CC)$(CCFLAGS)$(LDFLAGS)$(objects) obj/gcc8_a_support.o -o $@
@m68k-amiga-elf-objdump --disassemble -S $@ >$(OUT).s

obj/gcc8_a_support.o: support/gcc8_a_support.s
$(info Assembling $<)
@$(CC)$(CCFLAGS)$(ASFLAGS) -xassembler-with-cpp -c -o $@$(CURDIR)/$<

clean:
$(info Cleaning...)
@del /q obj $(OUT).* 2>nul || rmdir obj 2>nul || ver>nul

-include$(objects:.o=.d)

$(cpp_objects) : obj/%.o : %.cpp
@if not exist "$(call forward-to-backward,$(dir$@))" mkdir $(call forward-to-backward,$(dir$@))
$(info Compiling $<)
@$(CC)$(CCFLAGS) -c -o $@$(CURDIR)/$<

$(c_objects) : obj/%.o : %.c
@if not exist "$(call forward-to-backward,$(dir$@))" mkdir $(call forward-to-backward,$(dir$@))
$(info Compiling $<)
@$(CC)$(CCFLAGS) -c -o $@$(CURDIR)/$<



Samurai_Crow 03 January 2021 11:29

I don't think Bartman's GCC10 installation even supports the C++ stdlib option. It's for writing hardware-banging demo code. If you need to allocate memory, use AllocVec().

Emufr3ak 03 January 2021 11:35

C++ is supported since Version 0.8.0.


A workaround to avoid the new keyword might do the job too.

Samurai_Crow 03 January 2021 11:39

If exception handling is the culprit, try adding std::noexcept to the new allocations and check for null allocations in your code.

Ernst Blofeld 03 January 2021 11:40

What Samurai_Crow said, there is no stdlib, so you'll not be able to port most things.

But what kind of code is this, and how far can you get if you leave the -nostdlib flag in?

Emufr3ak 03 January 2021 12:13

I can define classes and access them. As soon as I use the new operator I get linker errors (undefined symbol). Turns out the new operator uses malloc which in turn requires stdlib. Maybe I could simulate the behavior of new like this.

1) Use memory allocation function for Amiga OS
2) Call constructor manually

Not sure how to know how much memory to allocate though.

It‘s the Wintermute Adventure engine I‘m trying to port. My goal is not to be fully compatible with recent games, but to have a decent engine on the Amiga for new games. So it’s going to be a minimal port.

Ernst Blofeld 03 January 2021 12:24

Quote:

Originally Posted by Emufr3ak (Post 1450354)
I can define classes and access them. As soon as I use the new operator I get linker errors (undefined symbol). Turns out the new operator uses malloc which in turn requires stdlib. Maybe I could simulate the behavior of new like this.

1) Use memory allocation function for Amiga OS
2) Call constructor manually

Not sure how to know how much memory to allocate though.

It‘s the Wintermute Adventure engine I‘m trying to port. My goal is not to be fully compatible with recent games, but to have a decent engine on the Amiga for new games. So it’s going to be a minimal port.

If it's only malloc that you need, what happens if you provide your own?

Code:

void *malloc(size_t size) {
    return AllocVec(size, NULL);
}


Emufr3ak 03 January 2021 19:23

Thank Ernst. This definitely pointed me in the right Direction. It's possible to implement new operators for new and delete. This did the trick.

void * operator new(size_t size)
{
KPrintF("Operator new Called");
void *p = AllocMem( size, MEMF_ANY);
return p;
}

void operator delete(void * p)
{
KPrintF("Operator delete Called");
FreeMem( p, sizeof(p));
}

Samurai_Crow 04 January 2021 01:09

If p is a void pointer, what is sizeof(p)?

pipper 04 January 2021 03:39

4 - which is definitely wrong here

alpine9000 04 January 2021 04:15

Just use AllocVec and FreeVec instead as they track the allocation size

Samurai_Crow 04 January 2021 04:24

Quote:

Originally Posted by alpine9000 (Post 1450567)
Just use AllocVec and FreeVec instead as they track the allocation size

I agree. It's simple and effective and only requires Kickstart 2 or more.

Ernst Blofeld 04 January 2021 09:54

I've just had a go at compiling a simple test class, and it looks like there's more stuff missing, I get "undefined reference to `_Znwm'" and "undefined reference to `_ZdlPvm'", and I'm not even doing anything yet.

If you're working with a bunch of existing code and need full C++ support, maybe Bebbo's gcc build is a better option.

Emufr3ak 04 January 2021 10:17

Thanjs for pointing out the Problem with Sizeof. Will switch to AllocVec and FreeVec. Up till now my Classes work fine with this solution. Can I have a look at yours Ernst.

Ernst Blofeld 04 January 2021 10:27

Quote:

Originally Posted by Emufr3ak (Post 1450595)
Can I have a look at yours Ernst.

Here's my main.cpp:

Code:

#include "support/gcc8_c_support.h"
#include <exec/execbase.h>

struct ExecBase * SysBase;

class TestClass {
    public:
        TestClass(int a);
        ~TestClass();

        void Test();
   
    private:
        int a;
};

TestClass::TestClass(int aa) {
    KPrintF("In TestClass::TestClass(int)");
    a = aa;
}

TestClass::~TestClass() {
    KPrintF("In TestClass::~TestClass()");
}

void TestClass::Test() {
    KPrintF("In TestClass::Test, a = %ld", a);
}

int main() {
    SysBase = *((struct ExecBase * *) 4UL);

    KPrintF("In main()");

    TestClass test1 = TestClass(1);
    test1.Test();

    TestClass * test2 = new TestClass(2);
    test2->Test();
    delete test2;
}


Emufr3ak 08 January 2021 15:29

I do have issues with virtual methods. It fails again at linking stage. Trying bebbos solution with eclipse. Do prefer the vscode experience though.

Emufr3ak 10 January 2021 16:01

Adding the flag "-shared-libgcc" did the trick. Now virtual methods work.

Ernst Blofeld 10 January 2021 16:09

Quote:

Originally Posted by Emufr3ak (Post 1452335)
Adding the flag "-shared-libgcc" did the trick. Now virtual methods work.

Yay!

Please let us know how you get on. While I hate C++, I'm getting tired of what I have to go through without it.

nyteshade 12 March 2021 18:35

Quote:

Originally Posted by alpine9000 (Post 1450567)
Just use AllocVec and FreeVec instead as they track the allocation size


Do people here not use CreatePool(), AllocPooled() and DeletePool?


Sent from my iPhone using Tapatalk

Ernst Blofeld 12 March 2021 19:03

Quote:

Originally Posted by nyteshade (Post 1469706)
Do people here not use CreatePool(), AllocPooled() and DeletePool?


Sent from my iPhone using Tapatalk

Is this available on Kickstart 1.2?


All times are GMT +2. The time now is 16:25.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.05234 seconds with 11 queries