I am trying to create a static library out of a C++ file I have written for a homework assignment. I am using macOS and I got the impression that one of the best ways to do this is to use Cmake and CLion, so that's what I've been trying. I really have no clue what I'm doing, as I'm completely new to C++, CLion, Cmake, and creating static libraries. In addition to that, the assignment is brief and kind of vague. There is a sentence that translates to "Add a reference from [project_name] to the static library so that the code is available when compiling".
I assume this means I am supposed to link the library to my main file so that I can use the code it contains instead of including the implementation and header files themselves (But perhaps someone reading this has a different understanding of that sentence). I have created the library, but I don't understand how to link it to my project/main file or what exactly I need to include at the top of the main file. My CMakeLists.txt file looks like this:
cmake_minimum_required(VERSION 3.16)
project(ecg_tree)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(SOURCE_FILES main.cpp node.cpp)
add_library(tree ${SOURCE_FILES})
include(FindPkgConfig)
find_package(tree REQUIRED)
include_directories(${TREE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${TREE_LIBRARY})
The tutorial I consulted to help with this said to make a file called "find[library_name].cmake" to help find the library, so this is what my cmake file looks like:
set(FIND_TREE_PATHS
/Users/ericbush/Documents/ecgt)
find_path(TREE_INCLUDE_DIR node.h
PATH_SUFFIXES include
PATHS ${FIND_TREE_PATHS})
find_library(TREE_LIBRARY
NAMES tree
PATH_SUFFIXES lib
PATHS ${FIND_TREE_PATHS})
I put this file in a folder called "cmake" within the project. When I try to build the project after making this file I get the following error:
-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
CMake Error at CMakeLists.txt:15 (target_link_libraries):
Cannot specify link libraries for target "ecg_tree" which is not built by
this project.
The last step seemed kind of convoluted to me and I have the feeling there is a better, simpler solution, but like I said I'm struggling to understand what exactly I'm supposed to do and our instructors don't really seem interested in helping us if we're not using Windows. If anyone wants to suggest a solution that forgoes the inclusion of this second file, I'm all ears.
The .a file is in a folder called "lib" that is in a folder called "ecgt". The "ecgt" folder also contains a folder called "include" that contains the header file for the file I'm trying to make into the library. The project initially consisted of three files: main.cpp, node.cpp, and node.h. Thank you for reading. I would be thankful for any responses that could help me figure out how to do this.