Hello, I want to build a component which needs both C++ and C files, however, I encountered some problem when I compiling them together. Here's the error: libc.lib.so: undefined reference to `Libc::Component::construct(Libc::Env&)' collect2: error: ld returned 1 exit status
For brief, in the C++ file (main.cc ), I would use a function in the C file , so I want to compile them together. In the main.cc file ,I add " extern "C" { # include cfile.h } " . In the cfile.h there exist the C function declaration ,let's say " fun() " , and I would use fun() in the main.cc. And in the target.mk, I set SRC_C = xx.c xxx.c (all the C files to be used ) and add LIBS+= libc to use C library which the fun() function would use. And I get the error message above, so my question is how can I compile the C++ and the C files together? Any help will be greatful, thanks in advance!
------------------ Best wishes
Hello lzSun
On 17.09.2018 11:30, lzSun wrote:
Hello, I want to build a component which needs both C++ and C files, however, I encountered some problem when I compiling them together. Here's the error: libc.lib.so: undefined reference to `Libc::Component::construct(Libc::Env&)' collect2: error: ld returned 1 exit status
In a Genode component using the libc, the function Libc::Component::construct() is required. It is there to initialize some things that are needed by libc programs (eg. the command line parameters).
There are two methods to to provide this function: - link the posix library to your component - write it yourself
The posix library is a wrapper that provides the Libc::Component::construct() function and calls the main() method of the program, after setting up some variables that are normally needed by posix programs.
Best regards, Pirmin
Hello lzSun,
On Mon, Sep 17, 2018 at 05:30:17PM +0800, lzSun wrote:
I want to build a component which needs both C++ and C files, however, I encountered some problem when I compiling them together. Here's the error: libc.lib.so: undefined reference to `Libc::Component::construct(Libc::Env&)' collect2: error: ld returned 1 exit status
This error is not related to your mix of C and C++ sources in one program. The cause is that you missed to implement the mentioned function Libc::Component::construct(), which is the entrypoint for libc-based components in Genode. The equivalent is the main() function in the C standard and you may restore this behavior in Genode by adding
LIBS += posix
to your library dependencies in target.mk for your program.
As a background, we deprecated the use of the original main() function for Genode components some years ago because it depends on global resources, which are accessible to all software modules for the whole runtime of the component. In contrast, Genode components come alive in the Genode::construct() function, which is passed a reference to the Genode::Env for dedicated access to program resources. If further moduels need access to resources references must be passed explcitly, which prevents unintended side effects. Later we adapted this pattern for libc-based components too and the _posix_ library is there just for backwards compatibility of ported traditional applications.
Greets