Building the unix port as a library (.a file)

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Building the unix port as a library (.a file)

Post by cduran » Wed Jul 06, 2022 6:59 pm

I've had success building the minimal port as a library (.a) but when I try the same with the Unix port I get a lot of undefined objects despite the source compiling properly.

To make the library I add the following:

Code: Select all

staticlib: $(OBJ)
	$(Q)$(AR) -r -o libmicropython.a $(OBJ)
to the Unix port makefile. When I try to link in the library (I make sure I have all the header files) to my application I get a slew of undefined functions and structs. When I look at the contents of the mp object files (using nm) it shows that those functions and structs are indeed not defined in the object files. I don't think the problem is with AR, there must be something I can't pinpoint myself in the Unix port makefile that is causing this.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Building the unix port as a library (.a file)

Post by jimmo » Thu Jul 07, 2022 12:43 am

cduran wrote:
Wed Jul 06, 2022 6:59 pm
I've had success building the minimal port as a library (.a) but when I try the same with the Unix port I get a lot of undefined objects despite the source compiling properly.
Can you post some examples of the missing symbols and also a minimal example of a program linking against your static library.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Building the unix port as a library (.a file)

Post by cduran » Fri Jul 08, 2022 12:41 pm

jimmo wrote:
Thu Jul 07, 2022 12:43 am
cduran wrote:
Wed Jul 06, 2022 6:59 pm
I've had success building the minimal port as a library (.a) but when I try the same with the Unix port I get a lot of undefined objects despite the source compiling properly.
Can you post some examples of the missing symbols and also a minimal example of a program linking against your static library.
These are the undefined symbols. Note they are also undefined in their corresponding object files.

Code: Select all

undefined reference to `mp_hal_stdout_tx_strn_cooked'
undefined reference to `mp_builtin_open_obj'
undefined reference to `gc_collect'
undefined reference to `mp_hal_set_interrupt_char'
undefined reference to `mp_module_time'
undefined reference to `mp_unix_free_exec'
undefined reference to `mp_stderr_print'
undefined reference to `mp_module_uselect'
undefined reference to `mp_module_machine'
undefined reference to `mp_module_os'
undefined reference to `mp_unix_alloc_exec'
I'll post how it's being linked shortly. However, wouldn't the problem be in how the library is being built? The symbols are undefined in the static library, so no matter how the static library is link to my program, those symbols are not there.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Building the unix port as a library (.a file)

Post by jimmo » Mon Jul 11, 2022 6:10 am

cduran wrote:
Fri Jul 08, 2022 12:41 pm
These are the undefined symbols. Note they are also undefined in their corresponding object files.
I added exactly the fragment you posted to unix/Makefile and I get (for example, gc_collect)

Code: Select all

$ nm libmicropython.a | grep 'gc_collect'
                 U gc_collect
0000000000000000 T gc_collect_end
0000000000000000 T gc_collect_root
0000000000000000 T gc_collect_start
                 U gc_collect
0000000000000000 D gc_collect_obj
0000000000000000 t py_gc_collect
0000000000000000 T gc_collect                           <------------
                 U gc_collect_end
                 U gc_collect_start
                 U gc_collect_root
                 U gc_collect_root
                 U gc_collect_root

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Building the unix port as a library (.a file)

Post by cduran » Thu Jul 21, 2022 4:59 pm

jimmo wrote:
Mon Jul 11, 2022 6:10 am
cduran wrote:
Fri Jul 08, 2022 12:41 pm
These are the undefined symbols. Note they are also undefined in their corresponding object files.
I added exactly the fragment you posted to unix/Makefile and I get (for example, gc_collect)

Code: Select all

$ nm libmicropython.a | grep 'gc_collect'
                 U gc_collect
0000000000000000 T gc_collect_end
0000000000000000 T gc_collect_root
0000000000000000 T gc_collect_start
                 U gc_collect
0000000000000000 D gc_collect_obj
0000000000000000 t py_gc_collect
0000000000000000 T gc_collect                           <------------
                 U gc_collect_end
                 U gc_collect_start
                 U gc_collect_root
                 U gc_collect_root
                 U gc_collect_root
Thank you for the help...finally found out what my problem was. I wasn't including any of the lib files into the compile. I simply removed the main function from main.c and compiled with main.c included. That did the trick.

Post Reply