Linking my static library

Hi, I have written a C++ library as a beginner project. I have created a Makefile with the instructions to compile the code as a static library (make static) and as a dynamic library (make dynamic).

The code is in this repo, with the Makefile in the root: https://github.com/lazaro92/CliWidget/tree/develop
(The develop branch has the last changes)

Later, I have tried to add in a simple C++ script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <vector>
#include <iostream>
#include <string>

#include <CliWidget>

int main() {
    CliWidget::Select select(std::vector<std::string> {"One", "Two", "Three"});
    CliWidget::MultiSelect multiSelect(std::vector<std::string>{"Cat", "Dog", "Hamster", "Bird", "Fish"});

    select.display(std::cout);

    CliWidget::Text text1("Selected index is " + std::to_string(select.getSelectedIndex()) + "");
    CliWidget::Text text2("Selected value is " + select.getSelectedValue() + "");

    text1.setBackgroundColor(CliWidget::BackgroundColor::GREEN);
    text1.setForegroundColor(CliWidget::ForegroundColor::BLUE);
    text1.setUnderline(true);
    text1.setBold(true);

    text2.setItalic(true);
    text2.setBlink(true);

    text1.display(std::cout);
    text2.display(std::cout);

    multiSelect.display(std::cout);

    std::string strText3 = "Selected indexes are ";
    std::string strText4 = "Selected values are ";

    for (int index : multiSelect.getSelectedIndexes()) {
        strText3 += std::to_string(index) + " ";
    }

    for (std::string value : multiSelect.getSelectedValues()) {
        strText4 += value + " ";
    }

    CliWidget::Text text3(strText3);
    text3.display(std::cout);

    CliWidget::Text text4(strText4);
    text4.display(std::cout);

    CliWidget::Text textPwd("Insert a Password:");
    CliWidget::InputPassword inputPsw;
    inputPsw.setRegex("^.{6,}");

    textPwd.display(std::cout);
    inputPsw.display();

    CliWidget::Text textResult("The entered password is " + inputPsw.getValue());
    textResult.setBold(true);
    textResult.display(std::cout);

    if (!inputPsw.check()) {
        CliWidget::Text textResult("The entered password is short");
        textResult.display(std::cout);
    }

    return 0;
}


and compile with: g++ -static main.cpp -lCliWidgets -o test_program

but it throws the error:

main.cpp:5:10: fatal error: CliWidget: No such file or directory
#include <CliWidget>

What am I doing wrong? I'ts the first time I'm making a library.
I don't see any file named CliWidget in your repo. You don't #include a library, you add it to the project, also a library shouldn't contain a function called main().


Sorry, the main you see in the src folder is only for test purpose with the classes of the library. So the Makefile has 4 functions:
- make: builds a program with the main to test the code.
- make static: builds the static library (without main).
- make dynamic: builds the dynamic library (without main) .
- make clean: erase all .o files, library files and output.

In a separate folder outside the repository I have two files:
- main.cpp: the main script you see in the first comment.
- libCliWidget.a: the static library compiled using the command make static

In this folder, using g++ is where I try to compile the main.c with the library using g++ -static main.cpp -lCliWidgets -o test_program
Okay so where is a header file named <CliWidget> located?

What does this header file contain?

Okay I found the problem. I had never built a static library with C++. I didn't know that I needed the header files within the .a file. Now, in the test project I have added the headers and it works now. Obviusly I have changed the #include <CliWidget> for the project headers.

Thanks a lot for the help :)
Topic archived. No new replies allowed.