Search code examples
c++buildcmakedependencieslibraries

Can cmake include a library that has header and lib files but no .cmake file?


Here is the directory structure of the library (specifically tre regex) I want to include:

/home/dave/some/directory/
    /lib
        libtre.a
    /include/tre
        regex.h
        tre-config.h
        tre.h

I cannot figure out how to make find_package work with this. Everything I tried gives an error message telling how to specify the path to a .cmake file, but there is no cmake file. How do you include a simple library like this in CMakeLists.txt?

Here is my most recent attempt at including it in CMakeLists.txt:

include_directories(/home/dave/some/directory/include/tre)
link_directories(/home/dave/some/directory/lib/)
find_package(tre)
target_link_libraries(the_program PRIVATE tre)

Solution

  • These lines should do the job:

    target_include_directories(your_project PRIVATE /path/include/tre)
    target_link_directories(your_project PRIVATE /path/lib)
    target_link_libraries(your_project PRIVATE libtre.a)
    

    Alternatively:

    add_library(tre STATIC IMPORTED)
    set_target_properties(tre PROPERTIES IMPORTED_LOCATION /path/lib/libtre.a)
    set_target_properties(tre PROPERTIES INTERFACE_INCLUDE_DIRECTORIES /path/include/tre)
    
    target_link_libraries(your_project PRIVATE tre)