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)

Parents
  • 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  ,

    You should not do this within Segger Embedded studio. Make the new file outside of it, and add it in CMakeLists.txt. Then the Segger embedded studio project is generated for you correctly.

    How do I make a new file outside of it and add it in CMakeLists.txt.?

    Can you kindly suggest.!

    Have attached my hello_world sample project for your reference with main.cpp

    7041.CPP_Test.7z

  • Hello ,

    Have attached my sample workspace
    Sample.7z

    I am using the same workspace that you suggested 

    Only change I have made is defined a new folder "temp" within and added "ex_Class.h" and "ex_Class.cpp".
    Then I am trying to #include "ex_class.h" in "main.cpp", when I am getting the error stating:
    cannot open source file "ex_class.h"

    Kindly help me solve this error.

  • Hi,

    As long as the headers and source files are all in the same folder there is no need to add the folders to the include path. But if not, you need to add them. Here I have modified your CMakeLists.txt to add both folders to the include path:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world)
    
    # Add source directories
    file(GLOB app_files temp/*)
    target_sources(app PRIVATE ${app_files})
    
    file(GLOB app_sources src/*)
    target_sources(app PRIVATE ${app_sources})
    
    # Add inlcude directories
    target_include_directories(app PRIVATE include temp)
    target_include_directories(app PRIVATE include src)
    

  • Hello ,

    Thank you, I worked out with something similar:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world)
    
    #file(GLOB app_files temp/*)
    #target_sources(app PRIVATE ${app_files})
    target_include_directories(app PUBLIC temp)
    add_subdirectory(temp)
    
    file(GLOB app_sources src/*)
    target_sources(app PRIVATE ${app_sources})
    
    zephyr_library_include_directories(.)

    Also, I defined a new CMakeLists.txt inside newly defined folder "temp" as:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    target_sources(app PRIVATE ex_class.cpp)
    
    


    And included in the project directory "CMakeLists.txt" as:
    add_subdirectory(temp)



    So my question is when to do this..? That is, to add & use 
    add_subdirectory(temp) in the project directory "CMakeLists.txt".?

  • If you add a new folder that you need to include from, then you need to do this. This is only the case for include paths you add yourself, though (so your folders with your files in them).

    This is not the case if you for instance add a library that already exists in the nRF Connect SDK. For any library/driver/etc. that is part of the nRF Connect SDK this is done for you, so you only need to enable it in your prj.conf. Finding it can be difficult, but here I suggest you refer to a relevant sample for what you want to do. You can also search the list of all configuration options.

Reply Children
No Data
Related