Have to import cpp files to run code

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Have to import cpp files to run code

    Hi,
    I am new to Segger Embedded Studio and have a problem. I am trying to make a lcd library which contains of a .cpp-file and a .h-file. When I include only lcd.h in main.cpp it can´t compile. (Receive: undefined reference to `LCD::init(unsigned char)' error). But when I include lcd.cpp also, everything work fine. I think it is only a setting to also compile cpp files, but I cant find it.

    Im using gcc with NRF52832 devkit.

    lcd.h:

    C Source Code

    1. #ifndef LCD_H_
    2. #define LCD_H_
    3. #include <stdint.h>
    4. #include <stdbool.h>
    5. class LCD
    6. {
    7. public:
    8. void init(uint8_t pin1);
    9. };
    10. #endif
    Display All




    lcd.cpp:

    Source Code

    1. #include "lcd.h"
    2. void LCD::init(uint8_t pin1)
    3. {
    4. pin1 = 1;
    5. }




    main.cpp:

    Source Code

    1. #include "lcd.h"
    2. int main(void) {
    3. uint8_t pin1 = 26;
    4. LCD lcd;
    5. lcd.init(pin1);
    6. while (1);
    7. return 0;
    8. }
  • Hello Øyvind,

    Thank you for your inquiry.

    The lcd.cpp file must be part of your project. Otherwise the linker can't find the functions that are referenced.
    So simply add it by e.g. drag and drop to your project tree.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Thank you Nino.
    It sounds strange to do it this way but its ok.

    I also have another problem. I have installed "STLport Library Package" in order to include <iostream> and use to_string for example. Under tools>Show installed packages it is explained how to use STLPort:


    Usage
    The C++ STL functionality of STLPort 5.2.1 is provided in this package. To use STLPort when this package has been installed set the Libraries > STLport Library project property to either
    - Yes just for the STL features.
    - Yes with IOStream support for both STL features and IOStreams.

    But where do I find Libraries > STLport Library so I can edit the settings? It is probably a very newbie question...
  • Hello Øyvind,

    I also have another problem. I have installed "STLport Library Package" in order to include <iostream> and use to_string for example. Under tools>Show installed packages it is explained how to use STLPort:


    To get to the option you can either right click your project in the "Project Explorer" and select "Edit Options...". From there on navigate to the section "Libraries".
    There you can enable or disable the STLport Library.
    You can also use the "Search Options" function and type STL for example to navigate to the entry.
    Then double click "STLPort Library" and change the setting to "Yes".
    Alternatively you can use the drop down menu. Go to : Project-> Edit Options... and from there follow the steps described above.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.