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)

  • Hello ,

    Thank you so much, This I was able to work successfully work with on VSCode.

    Strangely though, as in below:

    There are some errors, yet the project is building fine. If you see in the snapshot, 

    #include <zephyr.h> is highlighted in red

    And also there is an error: {
    "resource": "/d:/sample_app/Sample/src/main.cpp",
    "owner": "C/C++",
    "code": "1696",
    "severity": 8,
    "message": "cannot open source file \"stddef.h\" (dependency of \"zephyr.h\")",
    "source": "C/C++",
    "startLineNumber": 7,
    "startColumn": 1,
    "endLineNumber": 7,
    "endColumn": 20
    }


    How can I solve this issue..?
  • Ubaid_M said:
    Thank you so much, This I was able to work successfully work with on VSCode.

    Good to hear.

    Ubaid_M said:
    There are some errors, yet the project is building fine.

    The error you posted in text and from the screenshot is the same. I have not been able to reproduce myself, but this looks like it is just from the intelligence in VS Code, and does not have any impact on the building of the project itself. I suggest you ignore it. Alternatively, look into the C_Cpp settings in your VS Code configuration (perhaps this thread has something useful).

  • Hello ,

    Thank you the info,
    Moving ahead, Request you to guide me on what are all the changes I will have to make,
    So as to call the file: 
    nCS\v1.7.0\zephyr\include\drivers\clock_control.h (#include clock_control.h)

    In "my_class.h" file that 

    Please see this sample: hello_world_cpp_additional_source_files.zip. It works well with NCS 1.7.0 and SES, and demonstrates C++ including adding a second file.

    you helped me with the other day.


    Thanks,

    Ubaid

  • Hi Ubaid,

    The clock control is available without the need to do any adjustments to prj.conf or similar. Simply add include statements and call the API functions you need. I suggest you refer to a relevant clock control tests to see an example.

    Please make a new thread for new questions like this in the future.

  • Hi ,

    Will create a new thread for that, 

    In this project you referred, I need to move 

    "my_class.h" & "my_class.cpp" into a separate folder known as "Sample\app_files".

    I did and made the following changes to 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)
    
    file(GLOB app_sources src/*)
    target_sources(app PRIVATE ${app_sources})
    
    file(GLOB app_files app_files/*)
    target_sources(app PRIVATE ${app_files})
    
    file(GLOB app_includes app_files/)
    target_include_directories(app PRIVATE include ${app_includes})
    
    
     
    Now the project is throwing build errors as:


    Kindly help me on how to accommodate the files' relocation changes in CMakeLists.txt to build successfully.
Related