English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 03 January 2021, 12:21   #1
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
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)/$<


Emufr3ak is offline  
Old 03 January 2021, 12:29   #2
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 47
Posts: 1,813
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().
Samurai_Crow is offline  
Old 03 January 2021, 12:35   #3
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
C++ is supported since Version 0.8.0.


A workaround to avoid the new keyword might do the job too.
Emufr3ak is offline  
Old 03 January 2021, 12:39   #4
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 47
Posts: 1,813
If exception handling is the culprit, try adding std::noexcept to the new allocations and check for null allocations in your code.

Last edited by Samurai_Crow; 03 January 2021 at 12:46.
Samurai_Crow is offline  
Old 03 January 2021, 12:40   #5
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
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?
Ernst Blofeld is offline  
Old 03 January 2021, 13:13   #6
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
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.
Emufr3ak is offline  
Old 03 January 2021, 13:24   #7
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by Emufr3ak View Post
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);
}
Ernst Blofeld is offline  
Old 03 January 2021, 20:23   #8
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
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));
}
Emufr3ak is offline  
Old 04 January 2021, 02:09   #9
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 47
Posts: 1,813
If p is a void pointer, what is sizeof(p)?
Samurai_Crow is offline  
Old 04 January 2021, 04:39   #10
pipper
Registered User

 
Join Date: Jul 2017
Location: San Jose
Posts: 326
4 - which is definitely wrong here
pipper is offline  
Old 04 January 2021, 05:15   #11
alpine9000
Registered User

 
Join Date: Mar 2016
Location: Australia
Posts: 869
Just use AllocVec and FreeVec instead as they track the allocation size
alpine9000 is offline  
Old 04 January 2021, 05:24   #12
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 47
Posts: 1,813
Quote:
Originally Posted by alpine9000 View Post
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.
Samurai_Crow is offline  
Old 04 January 2021, 10:54   #13
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
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.
Ernst Blofeld is offline  
Old 04 January 2021, 11:17   #14
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
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.
Emufr3ak is offline  
Old 04 January 2021, 11:27   #15
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by Emufr3ak View Post
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;
}
Ernst Blofeld is offline  
Old 08 January 2021, 16:29   #16
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
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 is offline  
Old 10 January 2021, 17:01   #17
Emufr3ak
Registered User

 
Join Date: Mar 2016
Location: Thun / Switzerland
Posts: 50
Adding the flag "-shared-libgcc" did the trick. Now virtual methods work.
Emufr3ak is offline  
Old 10 January 2021, 17:09   #18
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by Emufr3ak View Post
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.
Ernst Blofeld is offline  
Old 12 March 2021, 19:35   #19
nyteshade
Registered User

 
Join Date: Mar 2020
Location: Boulder Creek / USA
Posts: 33
Quote:
Originally Posted by alpine9000 View Post
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
nyteshade is offline  
Old 12 March 2021, 20:03   #20
Ernst Blofeld
<optimized out>

Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 304
Quote:
Originally Posted by nyteshade View Post
Do people here not use CreatePool(), AllocPooled() and DeletePool?


Sent from my iPhone using Tapatalk
Is this available on Kickstart 1.2?
Ernst Blofeld is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
amiga-gcc minimal output Fook42 Coders. C/C++ 17 02 May 2019 10:49
Amiga GCC 6.2 developer guide appreciated asymetrix Coders. C/C++ 19 12 October 2018 00:58
Req: gcc crosscompiler with amiga hunk support emufan Coders. C/C++ 5 02 October 2017 02:51
Amiga GCC Where to find it? _ThEcRoW request.Apps 10 02 March 2006 01:44
Amiga GCC Where can i find? _ThEcRoW request.Apps 1 22 October 2005 19:17

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 18:04.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.12991 seconds with 15 queries