0

I am currently trying to set up the opendnp3 C++ library as a static library. I've built the solution following their build guide for Windows and have been able to generate several .lib files which I assume to be the static libraries.

In a completely separate folder, I have the following files under the following folder structure:

C:/Development/C++/opendnp3/lib/ # .lib files are contained in this directory

C:/Development/pybexample/
--> CMakeLists.txt
--> src/
    --> test.cpp

I have the CMakeLists.txt configured as follows:

cmake_minimum_required(VERSION 2.8)
project(pybexample)

set(SOURCE_FILES src/test.cpp)

add_library(opendnp3 STATIC IMPORTED)
set_target_properties(opendnp3 PROPERTIES IMPORTED_LOCATION C:/Development/C++/opendnp3/lib/opendnp3.lib)

add_executable(pybexample ${SOURCE_FILES})
target_link_libraries(pybexample opendnp3)

Within test.cpp, I am simply calling:

#include <iostream>
#include "opendnp3/LogLevels.h"
using namespace std;

int main(void) {
    cout << "Hello world!" << endl;
    system("pause");
}

However, when I try to build test.cpp, I receive an error indicating: "Cannot open include file: 'opendnp3/LogLevels.h': No such file or directory". I feel like there must be something pretty basic that I've missed but I'm pretty new to using static libraries and with CMake so I can't seem to figure it out. Would anyone be able to help give some pointers as to why my include is failing?

cing
  • 11
  • 1
  • You did not add the include directory in your `CMakeLists.txt` – drescherjm Mar 01 '19 at 01:10
  • ^ Thank you for pointing that out! Turns out there were two problems. The first problem was the library hadn't actually compiled properly so I didn't actually have headers to include. The second problem was that I was missing the include_directories() command to my headers. After compiling the library properly and adding that line in my CMakeLists.txt, the problem appears to be resolved. – cing Mar 01 '19 at 16:18
  • I would suggest that you answer your own question. – drescherjm Mar 01 '19 at 16:21

1 Answers1

1

Turns out this was caused by two problems.

The first problem was that I needed to add a line to my CMakeLists.txt that would add the header files for opendnp3 to my project. This was accomplished by adding include_directories(<path_to_headers>) before the add_executable(pybexample ${SOURCE_FILES}) line.

However, in doing this, it also became clear that there was a second problem: I hadn't built the library properly as it didn't generate any headers with the library. Turns out I had overlooked the instructions to run the INSTALL project. After setting CMAKE_INSTALL_PREFIX and running the INSTALL project, the library and headers were generated and ready for use.

cing
  • 11
  • 1