Problems using SNMP++/Agent++ from another CMakeLists.txt

Good day!

I have to start saying that building and installing SNMP++ and AGENT++ works as expected without any issue.

That said, I have to build a simple SNMP Agent and I have mainly 2 requirements: 1) Do not install any file in the system, 2) Use CMake self contained build.

I have a folder structure and CMakeLists.txt as follows:

* MySnmpAgent
    - snmppp ---> SNMP++ sources
    - agentpp ---> Agent++ sources

My CMakeLists.txt

cmake_minimum_required(VERSION 3.25)

project(MySnmpAgent)

add_executable(MySnmpAgent
    src/main.cpp
    )

add_subdirectory(snmppp)
add_subdirectory(agentpp)


target_link_libraries(MySnmpAgent PUBLIC
    snmp++_static
    agent++_static
    )

I have found 2 issues.

The first one, I receive a build error:

libsnmp.h:268:11: fatal error: iostream.h: No such file or directory

This error is caused by the following line in both snmp++ and agent++ CMakeLists.txt:

check_include_files ("iostream" CNF_HAVE_IOSTREAM)

Since “iostream” is a C++ header file, the line would be:

include (CheckIncludeFileCXX)

...

check_include_file_cxx ("iostream" CNF_HAVE_IOSTREAM)

Probably the same fix needs to be applied to the line:

check_include_files ("cctype;cerrno;climits;csignal;cstddef;cstdio;cstdlib;cstring;ctime" CNF_STDCXX_98_HEADERS)

This solves the issue. It would be nice to have this fix applied for the next release.

So far so good.

The second issue is not that straightforward. Agent++ complains about SNMP++ root dir. The issue goes away if I precompile SNMP++ and provide SNMP_PP_ROOT_DIR but does not look to me like the CMake-ish way of building the code. I would like to be able to just do (example):

cmake <path-to-my-CMakeLists.txt> [-DCMAKE_BUILD_TYPE=Debug]

And then build both SNMP++ and AGENT++ in the following make step:

make

I can probably solve the issue by issuing something like:

execute_command(
    COMMAND "cmake <path_to_snmppp>"
    WORKING DIRECTORY ${SNMP_BUILD_DIR}
    )

Would certainly work if there is no other option, but it is not what I am looking for since that would trigger the build of SNMP++ when the cmake command is triggered for my CMakeLists.txt

Is there a way of building both SNMP++ and AGENT++ in the ‘make’ step following to CMake command?

Thanks in advance

Hi,

it is easy to replace check_include_files with check_include_file_cxx for the single include, but I will have to think about the second usage with multiple files.

The module cmake/modules/Findsnmp_pp.cmake requires that the snmp++ library exists. I’m wondering how other multi library projects are doing this.
I could think of the following solution: Define the needed variables in your CMakeLists.txt and adapt Findsnmp_pp.cmake to just use the provided values instead of searching for the library.

Kind regards,
Jochen

Hi Jochen,

thank you for your response.

I get the point. Thanks for considering it.

If Snmp++ is already included in the project using its CMakeLists.txt via add_subdirectory(), the targets defined for Snmp++ will be there and it can be checked by (Example):

if (TARGET snmp++)
  # Snmp++ target is already defined
  # target_link_libraries will make available all public include directories, include files, and so on
  target_link_libraries(agent++ snmp++)
  target_link_libraries(agent++_static snmp++_static)
endif()

If you are fine with, I can make some changes to the CMakeLists.txt / find_snmppp.cmake and send you the code for your consideration.

My proposal will be 100% backwards compatible and work in the provided scenario.

Regards

That would be great. Thanks in advance and best regards,
Jochen