CMake build issue ‘undefined reference’ – Linker CPP main failed to link C component
Posted: Mon May 02, 2022 1:58 pm
Hello, I'm facing a problem when I try to compile my basic CPP project with a component written in C but it failed to build.
Previously I was using Platform IO environment but recently I switched to esp-idf (version 4.4.1) environment. This will come with new opportunities and challenges. What I’m trying to do is making my own esp32 project with C++ (not C), and make use of some external C components. The problem is that the linker, not the compiler, can’t build my solution. The Espressif Build System with CMake is new for me and I guess that I do something wrong here. I've made the code as small as possible to get it back to the essence of the issue. Could anyone tell me what I’m missing here?
The error is as follow:
in function `app_main': …main/test.cpp:6: undefined reference to `say_hello()' collect2.exe: error: ld returned 1 exit status
CMakeLists.txt
main\test.cpp
main\CMakeLists.txt
components\my_component\my_component.c
components\my_component\my_component.h
components\my_component\CMakeLists.txt
link to all project files:
Previously I was using Platform IO environment but recently I switched to esp-idf (version 4.4.1) environment. This will come with new opportunities and challenges. What I’m trying to do is making my own esp32 project with C++ (not C), and make use of some external C components. The problem is that the linker, not the compiler, can’t build my solution. The Espressif Build System with CMake is new for me and I guess that I do something wrong here. I've made the code as small as possible to get it back to the essence of the issue. Could anyone tell me what I’m missing here?
The error is as follow:
in function `app_main': …main/test.cpp:6: undefined reference to `say_hello()' collect2.exe: error: ld returned 1 exit status
CMakeLists.txt
Code: Select all
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
set(EXTRA_COMPONENT_DIRS src include lib components)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test)
Code: Select all
/* main program */
#include <stdio.h>
#include "my_component.h"
extern "C" void app_main(void) {
say_hello();
printf("app_main\n");
}
Code: Select all
idf_component_register(SRCS ${srcs} "test.cpp"
INCLUDE_DIRS ${include_dirs} "."
REQUIRES my_component)
Code: Select all
#include <stdio.h>
#include "my_component.h"
void say_hello(void) {
printf("Hello CPP\n");
}
Code: Select all
void say_hello(void);
Code: Select all
idf_component_register(SRCS "my_component.c" INCLUDE_DIRS ".")