quick c++ question

Go To Last Post
31 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a quick c++ question.  I have a c++ file, "test.cpp" with a function "void temp_test();" in it.  When I include the "test.h" file in main and try to call the "temp_test()" function I get the following error.

 

DEP ../../examples/getting_started_with_blue/main.c
CC ../../examples/getting_started_with_blue/main.c
LINK build/sama5d4-xplained/ddram/getting-started_with_blue.elf
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/main.o: In function `main':
D:\atmel-software-package-2.15\examples\getting_started_with_blue/../../examples/getting_started_with_blue/main.c:293: undefined reference to `temp_test'
collect2.exe: error: ld returned 1 exit status
make: *** [../../scripts/Makefile.rules:115: build/sama5d4-xplained/ddram/getting-started_with_blue.elf] Error 1

 

 

I've never used c++ before in an embedded environment and hope it is a silly error on my part.  Thanks lads!

Last Edited: Wed. Feb 5, 2020 - 03:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

found it lads, 

 

 

#ifdef __cplusplus
    extern "C" {
#endif
extern void temp_test(int);
#ifdef __cplusplus
    }
#endifs

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why is it main.c and not main.cpp if C++?

 

If you are mixing C and C++ then, because of name mangling, as you cross the C/C++ boundary you are going to need extern "C".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Why is it main.c and not main.cpp if C++?

 

If you are mixing C and C++ then, because of name mangling, as you cross the C/C++ boundary you are going to need extern "C".

 

Hi Clawson,

 

I tried that but the software frame work assumes that main is a c file and not a cpp.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson, I have one last error.  It is definitely due to the make file.  The error is listed below.  If I can get this error I will have most of the c++ working.

 

DEP ../../examples/getting_started_with_blue/test.cpp
CC ../../examples/getting_started_with_blue/test.cpp
LINK build/sama5d4-xplained/ddram/getting-started_with_blue.elf
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o:(.rodata._ZTI5Shape[_ZTI5Shape]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o:(.rodata._ZTI9Rectangle[_ZTI9Rectangle]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o:(.rodata._ZTI8Triangle[_ZTI8Triangle]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
collect2.exe: error: ld returned 1 exit status
make: *** [../../scripts/Makefile.rules:116: build/sama5d4-xplained/ddram/getting-started_with_blue.elf] Error 1

 

The code that causes this error is also below:

 

#include "test.h"
#include "stdio.h"

using namespace std;

class Shape {
protected:
    int width, height;

public:
    Shape(int a = 0, int b = 0) {
        width = a;
        height = b;
    }
    virtual int area() {
        printf("Parent class area\n");
        return 0;
    }
};
class Rectangle : public Shape {
public:
    Rectangle(int a = 0, int b = 0) :Shape(a, b) { }

    int area() {
    	printf("Rectangle class area\n");
        return (width * height);
    }
};

class Triangle : public Shape {
public:
    Triangle(int a = 0, int b = 0) :Shape(a, b) { }

    int area() {
    	printf("Triangle class area\n");
        return (width * height / 2);
    }
};

// Main function for the program
void temp_test(void) {
    Shape* shape;
    Rectangle rec(10, 7);
    Triangle  tri(10, 5);

    // store the address of Rectangle
    shape = &rec;

    // call rectangle area.
    shape->area();

    // store the address of Triangle
    shape = &tri;

    // call triangle area.
    shape->area();

    while (1);
}


 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The error disappears when I remove the virtual specifier.  Why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely the easiest thing for a C project is to rename "test.cpp" as "test.c"

 

The IDE will compile it as a C file.   Along with all the other C files in your project.

"test.h" can remain unchanged because you already have the C / C++ guard.

 

It is common for a C++ project to have a mixture of C++ and C files.

All the header files have the appropriate conditional guard for extern "C"

 

I am not aware of any C projects that have C++ files

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to clarify, I'm trying to setup a makefile to compile c++/

The following makefile command worked correctly for the above error:

 

-fno-rtti

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

New error that I need help with!

 

#include "test.h"
#include "stdio.h"

using namespace std;

double division(int a, int b) {
    if (b == 0) {
        throw "Division by zero condition!";
    }
    return (a / b);
}

void temp_test() {
    int x = 50;
    int y = 0;
    double z = 0;

    try {
        z = division(x, y);
        //cout << z << endl;
    }
    catch (const char* msg) {
        //cerr << msg << endl;
    }

    while (1);
}

ERROR

 

AR build/sama5d4-xplained/ddram/libsdmmc.a
CC ../../target/common/bootstrap.c
LINK build/sama5d4-xplained/ddram/target/bootstrap.elf
LINK build/sama5d4-xplained/ddram/getting-started_with_blue.elf
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o: In function `division(int, int)':
test.cpp:(.text+0x24): undefined reference to `__cxa_allocate_exception'
test.cpp:(.text+0x40): undefined reference to `typeinfo for char const*'
test.cpp:(.text+0x44): undefined reference to `typeinfo for char const*'
test.cpp:(.text+0x48): undefined reference to `__cxa_throw'
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o: In function `temp_test':
test.cpp:(.text+0xc0): undefined reference to `__cxa_end_cleanup'
test.cpp:(.text+0xcc): undefined reference to `__cxa_begin_catch'
test.cpp:(.text+0xd8): undefined reference to `__cxa_end_catch'
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o:(.ARM.extab+0xc): undefined reference to `__gxx_personality_v0'
./build/sama5d4-xplained/ddram/examples/getting_started_with_blue/test.o:(.ARM.extab+0x28): undefined reference to `typeinfo for char const*'
collect2.exe: error: ld returned 1 exit status
make: *** [../../scripts/Makefile.rules:118: build/sama5d4-xplained/ddram/getting-started_with_blue.elf] Error 1

18:10:48 Build Finished (took 1m:8s.256ms)
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And when you Google those (common!) errors what do you find? At a rough guess I'd say you aren't linking the C++ std lib.

Last Edited: Sun. Feb 2, 2020 - 06:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi clawson, I have googled the errors and found a few tips for the makefile that did not work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Found a fella that got past the error by including the following:

libgcc

what I've figured out is, if I link against libstc++.a and libsupc++.a
I can get the link errors to go away.libgcc

what I've figured out is, if I link against libstc++.a and libsupc++.a
I can get the link errors to go away.

 

 

So where do I include these in the makefile.  I've attached the makefile.

 

EDITED, this is not going to work - I think, lol.

I've got operator overloading and virtual functions working.  I'm working on exceptions now.

Attachment(s): 

Last Edited: Sun. Feb 2, 2020 - 09:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Still working on this.  If the project compiles and links without error but fails in the making of the OBJECT *.bin is the error definitely in the makefile?????

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'all' twice, did AS7 do that?

 

# ----------------------------------------------------------------------------
#         SAM Software Package License
# ----------------------------------------------------------------------------
# Copyright (c) 2015, Atmel Corporation
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the disclaimer below.
#
# Atmel's name may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
# DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

# default target
all::

include $(TOP)/scripts/Makefile.vars

BUILDDIR ?= ./build/$(TARGET)/$(VARIANT)

#-------------------------------------------------------------------------------
#		Rules
#-------------------------------------------------------------------------------

include $(TOP)/samba_applets/common/Makefile.inc
include $(TOP)/flash_loaders/applets/common/Makefile.inc
include $(TOP)/scripts/Makefile.config
include $(TOP)/arch/Makefile.inc
include $(TOP)/utils/Makefile.inc
include $(TOP)/target/Makefile.inc
include $(TOP)/drivers/Makefile.inc
include $(TOP)/lib/Makefile.inc
include $(TOP)/scripts/Makefile.freertos

vpath %.c $(TOP)
vpath %.cpp $(TOP)
vpath %.S $(TOP)

OBJS := $(addprefix $(BUILDDIR)/,$(gnu-cstartup-y) $(obj-y))
LIBS := $(addprefix $(BUILDDIR)/,$(lib-y))

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

.PHONY: all build clean size debug

all:: build

build: $(BUILDDIR)/$(BINNAME).elf \
	$(BUILDDIR)/$(BINNAME).symbols \
	$(BUILDDIR)/$(BINNAME).bin

ifeq ($(VARIANT),ddram)
CONFIG_BOOTSTRAP ?= y
ifeq ($(CONFIG_BOOTSTRAP),y)
$(BUILDDIR)/$(BINNAME).elf: $(BUILDDIR)/target/bootstrap.elf

$(BUILDDIR)/target/bootstrap.elf: $(BOOTSTRAP_OBJS) $(LIBS)
	@mkdir -p $(dir $@)
	$(ECHO) LINK $@
	$(Q)$(CC) $(LDFLAGS) $(CFLAGS_CPU) $(CFLAGS_DEFS) -T$(bootstrap-linker-script-y) -Wl,-Map,$(BUILDDIR)/bootstrap.map -o $@ $(BOOTSTRAP_OBJS) -Wl,--start-group $(LIBS) -Wl,--end-group
endif
endif

$(BUILDDIR):
	@mkdir -p $(BUILDDIR)


$(BUILDDIR)/%.d: %.cpp
	@mkdir -p $(dir $@)
	$(ECHO) DEP $<
	$(Q)$(CC) -fno-rtti -std=c++1z $(CFLAGS_CPU) $() $(CFLAGS_DEFS) -MM $< -MT $(basename $@).o -o $(basename $@).d

$(BUILDDIR)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(ECHO) CC $<
	$(Q)$(CC) -fno-rtti -std=c++1z $(CFLAGS_CPU) $() $(CFLAGS_DEFS) -c $< -o $@

$(BUILDDIR)/%.d: %.c
	@mkdir -p $(dir $@)
	$(ECHO) DEP $<
	$(Q)$(CC) $(CFLAGS) $(CFLAGS_CPU) $(CFLAGS_INC) $(CFLAGS_DEFS) -MM $< -MT $(basename $@).o -o $(basename $@).d

$(BUILDDIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(ECHO) CC $<
	$(Q)$(CC) $(CFLAGS) $(CFLAGS_CPU) $(CFLAGS_INC) $(CFLAGS_DEFS) -c $< -o $@

$(BUILDDIR)/%.d: %.S
	@mkdir -p $(dir $@)
	$(ECHO) DEP $<
	$(Q)$(CC) $(CFLAGS_ASM) $(CFLAGS_CPU) $(CFLAGS_INC) $(CFLAGS_DEFS) -MM $< -MT $(basename $@).o -o $(basename $@).d

$(BUILDDIR)/%.o: %.S
	@mkdir -p $(dir $@)
	$(ECHO) CC $<
	$(Q)$(CC) $(CFLAGS_ASM) $(CFLAGS_CPU) $(CFLAGS_INC) $(CFLAGS_DEFS) -c $< -o $@



$(BUILDDIR)/$(BINNAME).elf: $(OBJS) $(LIBS) $(gnu-debug-lib-y) $(gnu-linker-script-y)
	@cp $(gnu-debug-lib-y) $(BUILDDIR)/target/
	$(ECHO) LINK $@
	$(Q)$(CC) $(LDFLAGS) $(CFLAGS_CPU) $(CFLAGS_DEFS) -T$(gnu-linker-script-y) -Wl,-Map,$(BUILDDIR)/$(BINNAME).map -o $@ $(OBJS) -Wl,--start-group $(LIBS) -Wl,--end-group -Wl,--no-undefined

$(BUILDDIR)/$(BINNAME).symbols: $(BUILDDIR)/$(BINNAME).elf
	$(Q)$(NM) $< >$@

$(BUILDDIR)/$(BINNAME).bin: $(BUILDDIR)/$(BINNAME).elf
	$(ECHO) OBJCOPY $@
	$(Q)$(OBJCOPY) -O binary $< $@

clean:
	@rm -rf $(BUILDDIR) settings
	@rm -f $(BINNAME).eww $(BINNAME)_$(TARGET).ewp $(BINNAME)_$(TARGET).ewd $(BINNAME)_$(TARGET).ewt $(BINNAME)_$(TARGET).dep

size: $(BUILDDIR)/$(BINNAME).elf
	@$(SIZE) $(OBJECTS) $(BUILDDIR)/$(BINNAME).elf

debug: $(BUILDDIR)/$(BINNAME).elf
	$(Q)$(GDB) -cd $(BUILDDIR) -x "$(realpath $(gnu-debug-script-y))" -ex "reset" -readnow -se $(realpath $(BUILDDIR)/$(BINNAME).elf)

include $(TOP)/scripts/Makefile.iar

It is just text, I am to tired to look for the zipper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ron_sutherland wrote:

include $(TOP)/lib/Makefile.inc

That looks like the most interesting thing in the whole file - but we don't see the contents :-(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yet again we find an ARM-related thread from this user. You forum veterans are addressing this. Am I missing something?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.