This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

fault with calling int main( )

Hello,

I modified a sample app "blinky" on segger, to include main.cpp instead of main.c
Then when I built, I got a compile time error stating in main.cpp "void main(void)" should be changed to "int main( )"
I changed and it compiled successfully.

Now when I am running it on the board, the execution is not entering "int main( )" rather the execution is straight away entering fault_s.S, as in the screenshot:



Kindly help me in understanding why this is happening so and how I can get the execution to return to "int main( )" ..?


PS. I am using blinky sample app on segger, only changes I made is 
- Excluded main.c from the build
- Added main.cpp (Attaching for your reference)
- Added main.cpp in CMakeLists.txt (Attaching for your reference)

It is building successfully.



main.cpp


#include "stdio.h"
#include "stdint.h"

/**** 1. Demonstration On dynamic allocation  ****/
class Sample
{
  Sample* cPtr;
  public:
  Sample()
  {
    printf("ctor called \n");
    cPtr = new Sample();
  }
  
  ~Sample()
  {
    printf("dtor called \n");
    delete cPtr;
    cPtr = nullptr;
  }

};

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

//#include <zephyr.h>
//#include <device.h>
//#include <devicetree.h>
//#include <drivers/gpio.h>

///* 1000 msec = 1 sec */
//#define SLEEP_TIME_MS   1000

///* The devicetree node identifier for the "led0" alias. */
//#define LED0_NODE DT_ALIAS(led0)

//#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
//#define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
//#define PIN	DT_GPIO_PIN(LED0_NODE, gpios)
//#define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)
//#else
///* A build error here means your board isn't set up to blink an LED. */
//#error "Unsupported board: led0 devicetree alias is not defined"
//#define LED0	""
//#define PIN	0
//#define FLAGS	0
//#endif
       void func(void)
       {
        Sample obj;
       }

int main(void)
{
	const struct device *dev;
	bool led_is_on = true;
	int ret;

        func();

	//dev = device_get_binding(LED0);
	//if (dev == NULL) {
	//	return;
	//}

	//ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	//if (ret < 0) {
	//	return;
	//}

	//while (1) {
	//	gpio_pin_set(dev, PIN, (int)led_is_on);
	//	led_is_on = !led_is_on;
	//	k_msleep(SLEEP_TIME_MS);
	//}

        return 0;
}

CMakeLists.txt

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE D:/CPP_Test/cpp_files/main.cpp src/main.c)

  • Hi,

    Have you set CONFIG_CPLUSPLUS=y in your prj.conf? I recommend you take a look at the C++ Synchronization sample (which is the only C++ sample for now).

  • Hello ,

    Yes i have set the CPLUS_PLUS feature, I have also set below:

    CONFIG_CPLUSPLUS=y
    CONFIG_HEAP_MEM_POOL_SIZE=16384
    CONFIG_NEWLIB_LIBC=y
    CONFIG_LIB_CPLUSPLUS=y

    But, my problem is that execution is not entering "int main( )" in main.cpp

  • I see. I am not able to explain that, as it looks like the basic is in place. However, I do not know the details of your configuration (NCS version, toolchain version, etc). Can you test the sample I referred to from nRF Connect SDK 1.7.0 and using the toolchain manager so that you know the toolchain is good? That should work and does on my side.

    Update: I just noticed that you add both main.c and main.cpp in your CMakeLIsts.txt:

    target_sources(app PRIVATE D:/CPP_Test/cpp_files/main.cpp src/main.c)

    you should have got a linker error if main is defined in both so it is a bit odd, but a smoking gun.

  • I see. I am not able to explain that, as it looks like the basic is in place. However, I do not know the details of your configuration (NCS version, toolchain version, etc). Can you test the sample I referred to from nRF Connect SDK 1.7.0 and using the toolchain manager so that you know the toolchain is good? That should work and does on my side.

    Update: I just noticed that you add both main.c and main.cpp in your CMakeLIsts.txt:

    target_sources(app PRIVATE D:/CPP_Test/cpp_files/main.cpp src/main.c)

    you should have got a linker error if main is defined in both so it is a bit odd, but a smoking gun. (also, you probably use relative paths to make your project portable, but that is a different topic).

  • Hello Einar,

    I am trying to work it out, 
    By starting with a simplest hello_world sample nCS app,

    I copied contents of main.c into main.cpp, then made changes in CMakeLists.txt and built, I am getting this error:



    Here is  my CMakeLists.txt:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world)
    #src/main.c
    target_sources(app PRIVATE D:/CPP_Test/cpp_files/main.cpp)
    



    Further, i read that build.emProject modification is necessary when we define a new file, so I copied the fields under main.c as is into main.cpp with additional definition of "cpp_only_additional_options".

    Main.c had fields as:
          <file file_name="D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/samples/hello_world/src/main.c">
            <configuration
              Name="Common"
              build_dependency_file_name="$(ProjectDir)/CMakeFiles\app.dir\src\main.c.obj.d"
              build_exclude_from_build="Yes"
              build_object_file_name="CMakeFiles/app.dir/src/main.c.obj"
              c_only_additional_options="-DBUILD_VERSION=v2.6.99-ncs1;-DKERNEL;-DNRF52833_XXAA;-DUSE_PARTITION_MANAGER=0;-D_FORTIFY_SOURCE=2;-D__LINUX_ERRNO_EXTENSIONS__;-D__PROGRAM_START;-D__ZEPHYR__=1;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;-Izephyr/include/generated;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/.;-Os;-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-ffreestanding;-fno-common;-g;-gdwarf-4;-fdiagnostics-color=always;-mcpu=cortex-m4;-mthumb;-mabi=aapcs;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h;-Wall;-Wformat;-Wformat-security;-Wno-format-zero-length;-Wno-main;-Wno-pointer-sign;-Wpointer-arith;-Wexpansion-to-defined;-Wno-address-of-packed-member;-Wno-unused-but-set-variable;-Werror=implicit-int;-fno-asynchronous-unwind-tables;-fno-pie;-fno-pic;-fno-strict-overflow;-fno-reorder-functions;-fno-defer-pop;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/samples/hello_world=CMAKE_SOURCE_DIR;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr=ZEPHYR_BASE;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0=WEST_TOPDIR;-ffunction-sections;-fdata-sections;-specs=nano.specs;-std=c99"
              c_preprocessor_definitions="BUILD_VERSION=v2.6.99-ncs1;KERNEL;NRF52833_XXAA;USE_PARTITION_MANAGER=0;_FORTIFY_SOURCE=2;__LINUX_ERRNO_EXTENSIONS__;__PROGRAM_START;__ZEPHYR__=1;BUILD_VERSION=v2.6.99-ncs1;KERNEL;NRF52833_XXAA;USE_PARTITION_MANAGER=0;_FORTIFY_SOURCE=2;__LINUX_ERRNO_EXTENSIONS__;__PROGRAM_START;__ZEPHYR__=1"
              c_system_include_directories=""
              c_user_include_directories="D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;zephyr/include/generated;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;zephyr/include/generated;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/."
              code_completion_options="-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h;-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h" />
          </file>

    For Main.cpp, added below makefile instructions just below Main.c instructions as follows:

          <file file_name="D:/CPP_Test/cpp_files/main.cpp">
            <configuration
              Name="Common"
              c_only_additional_options="-DBUILD_VERSION=v2.6.99-ncs1;-DKERNEL;-DNRF52833_XXAA;-DUSE_PARTITION_MANAGER=0;-D_FORTIFY_SOURCE=2;-D__LINUX_ERRNO_EXTENSIONS__;-D__PROGRAM_START;-D__ZEPHYR__=1;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;-Izephyr/include/generated;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/.;-Os;-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-ffreestanding;-fno-common;-g;-gdwarf-4;-fdiagnostics-color=always;-mcpu=cortex-m4;-mthumb;-mabi=aapcs;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h;-Wall;-Wformat;-Wformat-security;-Wno-format-zero-length;-Wno-main;-Wno-pointer-sign;-Wpointer-arith;-Wexpansion-to-defined;-Wno-address-of-packed-member;-Wno-unused-but-set-variable;-Werror=implicit-int;-fno-asynchronous-unwind-tables;-fno-pie;-fno-pic;-fno-strict-overflow;-fno-reorder-functions;-fno-defer-pop;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/samples/basic/blinky=CMAKE_SOURCE_DIR;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr=ZEPHYR_BASE;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0=WEST_TOPDIR;-ffunction-sections;-fdata-sections;-specs=nano.specs;-std=c99"
              c_preprocessor_definitions="BUILD_VERSION=v2.6.99-ncs1;KERNEL;NRF52833_XXAA;USE_PARTITION_MANAGER=0;_FORTIFY_SOURCE=2;__LINUX_ERRNO_EXTENSIONS__;__PROGRAM_START;__ZEPHYR__=1;BUILD_VERSION=v2.6.99-ncs1;KERNEL;NRF52833_XXAA;USE_PARTITION_MANAGER=0;_FORTIFY_SOURCE=2;__LINUX_ERRNO_EXTENSIONS__;__PROGRAM_START;__ZEPHYR__=1"
              c_system_include_directories=""
              c_user_include_directories="D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;D$(Zephyr_PATH);zephyr/include/generated;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;zephyr/include/generated;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/."
              code_completion_options="-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h;-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h" />
              cpp_only_additional_options="-DBUILD_VERSION=v2.6.99-ncs1;-DKERNEL;-DNRF52833_XXAA;-DUSE_PARTITION_MANAGER=0;-D_FORTIFY_SOURCE=2;-D__LINUX_ERRNO_EXTENSIONS__;-D__PROGRAM_START;-D__ZEPHYR__=1;-ID$(Zephyr_PATH);-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include;-Izephyr/include/generated;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/soc/arm/nordic_nrf/nrf52;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/lib/libc/newlib/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/nrf/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/cmsis/CMSIS/Core/Include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/drivers/include;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/hal/nordic/nrfx/mdk;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/hal_nordic/nrfx/.;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/SEGGER;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/modules/debug/segger/Config;-ID:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/modules/segger/.;-Os;-imacros;D:/CPP_Test/zephyr/include/generated/autoconf.h;-ffreestanding;-fno-common;-g;-gdwarf-4;-fdiagnostics-color=always;-mcpu=cortex-m4;-mthumb;-mabi=aapcs;-imacros;D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/include/toolchain/zephyr_stdint.h;-Wall;-Wformat;-Wformat-security;-Wno-format-zero-length;-Wno-main;-Wno-pointer-sign;-Wpointer-arith;-Wexpansion-to-defined;-Wno-address-of-packed-member;-Wno-unused-but-set-variable;-Werror=implicit-int;-fno-asynchronous-unwind-tables;-fno-pie;-fno-pic;-fno-strict-overflow;-fno-reorder-functions;-fno-defer-pop;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr/samples/basic/blinky=CMAKE_SOURCE_DIR;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0/zephyr=ZEPHYR_BASE;-fmacro-prefix-map=D:/Nordic_App/Nordic_SDK/nCS/v1.7.0=WEST_TOPDIR;-ffunction-sections;-fdata-sections;-specs=nano.specs;-std=c99"
          </file>

    Kindly help me resolve those.

Related