crosswinds
Posts: 5
Joined: Sun May 09, 2021 9:56 pm

CMake: Adding u8g2 library

Sun May 09, 2021 10:08 pm

Hi guys,

I have my toolchain set up for the pico under debian.

All is working and i have a blink project set up and tested.

So far so good.

Was going to attempt to port the u8g2 library to the pico for a specific LCD i use.
However, the CMake nightmare is making this hard.

Is anyone here familliar with the U8G2 LCD library and CMake that might help?

Ie i do have this in my current blink CMakeLists.txt:

Code: Select all

include(/home/pi/pico/pico-sdk/pico_sdk_init.cmake)
project(main)



pico_sdk_init()

add_executable(main main.c)


target_link_libraries(main pico_stdlib)

pico_add_extra_outputs(main)
and then i have the csrc directory from: https://github.com/olikraus/u8g2

How can i make this work? Never worked with CMake before.

Checking around for examples, there are hundreds of different ways, and different versions.


Any help or pointers regarding this is greatly appreciated!

Thank you
Last edited by crosswinds on Mon May 10, 2021 2:00 am, edited 1 time in total.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 638
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: CMake: Adding u8g2 library

Mon May 10, 2021 1:16 am

I mean it is pretty easy to add a library - simplest probably just by pointing at the source - it would be pretty similar to the CMakeLists.txt there, but they have written that assuming some other build system. Note you can do this in your CMakeLists.txt

Code: Select all

add_library(my_library INTERFACE)

set(PATH_TO_SOURCES /some/absolute/path) # could be relative to ${CMAKE_CURRENT_LIST_DIR} which is that absolute path to where you are
target_sources(my_library INTERFACE
	${PATH_TO_SOURCES}/foo.c
	${PATH_TO_SOURCES}/bar.c
)
target_include_directories(my_iibrary INTERFACE
	${PATH_TO_INCLUDE_FILES}
)
and then just add my_library to the target_link_libraries of your app.

crosswinds
Posts: 5
Joined: Sun May 09, 2021 9:56 pm

Re: CMake: Adding u8g2 library

Mon May 10, 2021 2:14 am

Thank you for your assistance.

But i am afraid that this is to general also actually.

Using your example, gives me "cannot specify sources for target "my_library" which is not build by this project"

I m really stuck, and after three days trying to create this cmake cr** my mind is getting a bit cloudy also.
Following guides, examples and nothing works. And every guide and tutorial is different.

Also in your example, each .c file is specified it seems. So for a library as u8g2 i would manually need to add over 20+ files?

martinkooij
Posts: 59
Joined: Sun Feb 28, 2021 5:22 pm
Location: The Netherlands

Re: CMake: Adding u8g2 library

Mon May 10, 2021 1:09 pm

Hi,

If you intend to port the library to work with existing code you can follow the compiling directions of kilograham above. Note, however, that the current library does not contain a PICO I2C port, so you have to fork the library and add the pico specific call_backs in addition to get it working.

(I found a guide how to do that in https://github.com/olikraus/u8g2/wiki/P ... U-platform. Didn't look into that though).

If you write new stuff and intend to do basic things (text, dots, fills, lines, ...) you could give my ss_oled port to PICO a go. To be found in https://github.com/martinkooij/pi-pico-ss-oled. You can also use this as an example how to structure CMakefiles for libraries. Hope it helps a bit.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 638
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: CMake: Adding u8g2 library

Mon May 10, 2021 8:49 pm

Using your example, gives me "cannot specify sources for target "my_library" which is not build by this project"
Presumably you misspelled it then in the add_library above then.
I m really stuck, and after three days trying to create this cmake cr** my mind is getting a bit cloudy also.
Following guides, examples and nothing works. And every guide and tutorial is different.
I only gave you one :-)
Also in your example, each .c file is specified it seems. So for a library as u8g2 i would manually need to add over 20+ files?
Well you can glob them by wildcard, but i figured you'd at least have looked in the CMakeLists.txt in the top of the proeject you linked which already lists all the C files!

crosswinds
Posts: 5
Joined: Sun May 09, 2021 9:56 pm

Re: CMake: Adding u8g2 library

Mon May 10, 2021 11:24 pm

Well guys ,thank you for sticking with me on this.

I think i got it to work now. Well at least compile clean. :mrgreen:

Will now begin to try porting it.

I have done this on a regular Pi, just because i wanted to try it without using wiringPI as that was the only port at the time.
Works great. However, since i just wanted to try my code out, i compiled it with *.c instead. So never bothered with Cmake before, as you might imagine :)

But this CMake issues got the better of me :roll:

Thank you again!

slimhazard
Posts: 13
Joined: Sat Apr 03, 2021 8:47 pm

Re: CMake: Adding u8g2 library

Tue May 11, 2021 9:37 pm

@crosswinds, can you share how you got it to work? Because I am now similarly baffled as how to use an INTERFACE library in an app that's designed to depend on it.

I took the effort to develop an INTERFACE library, since the SDK docs and various threads on the forum recommend it. The CMakeLists.txt for the library looks very much like the example posted by kilograham further up. I assumed that configuring cmake for an app that uses the library would be easy next step, but I'm stuck.

I hope I'm wrong, but the thread so far has given me the dark suspicion that the app configuration has to repeat everything that was in CMakeLists.txt for the library. Re-list all of the sources, include directories, everything. But that can't be right, can it?

If that's really the way it's supposed to be done, then I have to ask again what was the advantage of declaring INTERFACE libraries in cmake. Isn't the whole point of declaring dependencies in a software build system so that you don't have to do that? What if something changes in the library, new sources or renamed files or something like that? Do you then have to go hunting through every app that uses it, repeating the same changes everywhere? I must have misunderstood something.

Certainly the SDK manages to get it done, with the help of pico_sdk_import.cmake. But that's quite a thicket of cmake scripting, doing at least some things I don't need or want in my own project -- optionally fetching a repo from git, for example. All I want to do is identify another directory on my local system in which the INTERFACE library resides, and use that in my app. But unfortunately I haven't figured out which part of pico_sdk_import.cmake does just that. I've tried to find the answer in the CMake documentation and examples online, but I'm afraid I haven't figured it out.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 638
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: CMake: Adding u8g2 library

Tue May 11, 2021 11:55 pm

Yeah you don't need to duplicate stuff...

If you have the library in a submodule foo/ with foo/CMakeLists.txt

put this is your app CMakeLists.txt

Code: Select all

add_subdirectory(foo)
the foo here is a relative or absolute path, however if foo is non contained somewhere the the app directory

Code: Select all

add_subdirectory(/path/to/foo foo_build)
The second parameter just tells cmake where to put the build output files (relative to the app's build directory)

slimhazard
Posts: 13
Joined: Sat Apr 03, 2021 8:47 pm

Re: CMake: Adding u8g2 library

Wed May 12, 2021 8:09 am

@kilograham thanks, that did it, I owe you a beer.

If you're here looking for a working solution -- the magic that I was looking for is mostly in add_subdirectory():

Code: Select all

# in CMakeLists.txt

# Convenience to locate the library
set(MYLIB_PATH ${CMAKE_CURRENT_LIST_DIR}/../relative/path/to/mylib)

# Build mylib in the app's build/mylib, as with pico-sdk
add_subdirectory(${MYLIB_PATH} mylib)

# The app includes a header from mylib that defines mylib's interface.
target_include_directories(myapp INTERFACE
  mylib/include
)

# Does the trick
target_link_libraries(myapp
  pico_stdlib
  mylib
)
I will probably add a bit of cmake scripting that can get the location of the lib from an environment variable, as does pico_sdk_import.cmake, to make this less dependent on a specfic file layout.

Sorry that I got a bit whiny last time. I know this isn't supposed to be a cmake support forum. I've built a fair number of projects that were developed for cmake, and it's easy enough to set a parameter in a CMakeLists.txt that someone else already got to work. But developing your own in own project is another level, first time I've done it, so I'm getting the full beginner experience with the attendant frustrations.

crosswinds
Posts: 5
Joined: Sun May 09, 2021 9:56 pm

Re: CMake: Adding u8g2 library

Wed May 12, 2021 11:23 am

Hi guys,

So sorry for my late reply.

Glad to see you got it to work.

I can really share your frustration in this case, believe me :D

Anyway, here is my list, (Please note that this probably contains mistakes and faults. But someone may need it anyway) :

Code: Select all

cmake_minimum_required(VERSION 3.16)

include(/home/pi/pico/pico-sdk/pico_sdk_init.cmake)
project(main)

add_library(my_library INTERFACE)

set(PATH_TO_SOURCES /home/pi/Documents/Programmering/disptest/u8g2)

target_sources(my_library INTERFACE
	"csrc/u8x8_setup.c"
                   # "csrc/u8x8_u8toa.c"
                   # "csrc/u8x8_d_ssd1306_96x16.c"
                    "csrc/u8g2_d_memory.c"
                    "csrc/u8g2_selection_list.c"
                    "csrc/u8g2_hvline.c"
                   # "csrc/u8x8_d_ssd1306_128x64_noname.c"
                    "csrc/u8x8_message.c"
                   # "csrc/u8x8_d_ssd1306_48x64.c"
                   # "csrc/u8x8_d_ssd1305.c"
                    "csrc/u8g2_kerning.c"
                    "csrc/u8x8_input_value.c"
                   # "csrc/u8x8_d_st7586s_s028hn118a.c"
                    "csrc/u8g2_polygon.c"
                   # "csrc/u8x8_d_uc1604.c"
                   # "csrc/u8x8_d_ssd1306_128x32.c"
                   # "csrc/u8x8_d_ssd1322.c"
                   # "csrc/u8x8_d_sbn1661.c"
                    "csrc/u8g2_input_value.c"
                    "csrc/u8x8_8x8.c"
                    "csrc/u8x8_debounce.c"
                   # "csrc/u8x8_d_uc1638.c"
                    "csrc/u8g2_setup.c"
                   # "csrc/u8x8_d_ist3020.c"
                    "csrc/u8x8_display.c"
                   # "csrc/u8x8_d_ssd1317.c"
                    "csrc/u8x8_d_ks0108.c"
                    "csrc/u8g2_box.c"
#                    "csrc/u8x8_selection_list.c"
                   # "csrc/u8x8_d_ssd1326.c"
                   # "csrc/u8x8_d_st75256.c"
                   # "csrc/u8x8_d_ssd1309.c"
                    "csrc/u8x8_byte.c"
                   # "csrc/u8x8_d_ssd1607_200x200.c"
                   # "csrc/u8x8_d_pcd8544_84x48.c"
#                    "csrc/u8log.c"
                   # "csrc/u8x8_d_sh1107.c"
                    "csrc/u8log_u8x8.c"
                    "csrc/u8g2_message.c"
                   # "csrc/u8x8_d_uc1610.c"
                   # "csrc/u8x8_d_sh1106_72x40.c"
                    "csrc/u8g2_line.c"
                    "csrc/u8g2_intersection.c"
                    "csrc/u8g2_fonts.c"
                   # "csrc/u8x8_d_sh1106_64x32.c"
                   # "csrc/u8x8_d_sed1330.c"
                   # "csrc/u8x8_d_pcf8814_hx1230.c"
                   # "csrc/u8x8_d_il3820_296x128.c"
                   # "csrc/u8x8_d_uc1701_dogs102.c"
                    "csrc/u8g2_cleardisplay.c"
                   # "csrc/u8x8_d_ssd1325.c"
                    "csrc/u8g2_font.c"
                   # "csrc/u8x8_d_uc1601.c"
                   # "csrc/u8x8_d_pcf8812.c"
                    "csrc/u8x8_cad.c"
#                    "csrc/u8x8_d_uc1611.c"
                    "csrc/u8x8_string.c"
                    "csrc/u8g2_ll_hvline.c"
                    "csrc/u8x8_fonts.c"
                   # "csrc/u8x8_d_ld7032_60x32.c"
#                    "csrc/u8x8_d_max7219.c"
                   # "csrc/u8x8_d_st7588.c"
                   # "csrc/u8x8_d_ssd1606_172x72.c"
                   # "csrc/u8x8_d_st7586s_erc240160.c"
                    "csrc/u8x8_d_stdio.c"
                   # "csrc/u8x8_d_sh1122.c"
#                    "csrc/u8log_u8g2.c"
                    "csrc/u8g2_circle.c"
                   # "csrc/u8x8_d_ssd1306_64x48.c"
                    "csrc/u8g2_buffer.c"
                    "csrc/u8g2_bitmap.c"
                    #"csrc/u8x8_d_lc7981.c"
                    #"csrc/u8x8_d_st7920.c"
                    #"csrc/u8x8_d_t6963.c"
                    #"csrc/u8x8_d_st7567.c"
                    #"csrc/u8x8_d_ls013b7dh03.c"
                    #"csrc/u8x8_d_ssd1327.c"
                    #"csrc/u8x8_d_sh1108.c"
                    "csrc/u8x8_gpio.c"
                    #"csrc/u8x8_d_uc1608.c"
                    #"csrc/u8x8_d_uc1617.c"
                    #"csrc/u8x8_d_ssd1329.c"
                    #"csrc/u8x8_d_ssd1306_64x32.c"
                    "csrc/u8g2_d_setup.c"
                    #"csrc/u8x8_d_uc1701_mini12864.c"
#                    "csrc/u8x8_u16toa.c"
#                    "csrc/u8x8_d_a2printer.c"
                    #"csrc/u8x8_d_st7565.c"
			)

target_include_directories(my_library INTERFACE
	$PATH_TO_INCLUDE_FILES)

pico_sdk_init()

add_executable(main main.c)



target_link_libraries(main pico_stdlib my_library)

pico_add_extra_outputs(main)

I got it to work with the LCD also. Will try to remember and write a guide for any other lost tinkers out there :)

Return to “SDK”