drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

Compiling JsonCpp with Drogon in CMake #1769

Open ErikTheBerik opened 1 year ago

ErikTheBerik commented 1 year ago

I am working on a project that uses Drogon as the server. I've been running it in my macbook with no problem, compiling everything with AppleClang. I now needed to run some tests by compiling it using gcc but I got some errors.

After searching I realised that the problem came from using the Homebrew version of jsoncpp (which is compiled using clang) instead of compiling it from source (as mentioned in issue #1288 )

Since I need to do this in CMake, as I want to be able to easily compile it in clang again (without having to change the installed version in my computer), I tried doing what was mentioned in issue #804 but it didn't work (most likely my CMake setup is wrong).

Can somehow guide me a bit? I am currently adding drogon to my project using this:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(ExternalJsonCpp)
include(CPM)

CPMAddPackage(
        NAME drogon
        VERSION 1.8.4
        GITHUB_REPOSITORY drogonframework/drogon
        GIT_TAG v1.8.4
)

target_link_libraries(MyFinalProject drogon)

Inside the cmake folder there are 3 files, CPM.cmake (so I can use CPMAddPackage), FindJsoncpp.cmake (which just sets JsonCpp_FOUND) and ExternalJsonCpp.cmake.

The file ExternalJsonCpp.cmake was taken from here. From what I can see it basically downloads jsoncpp and installs it, I guess I need to change something in this file to make it work.

When I try to run cmake, I get error "fatal error: json/json.h: No such file or directory" even tho ${JSONCPP_INCLUDE_DIR} and ${JSONCPP_LIBRARY} are properly set (I print them out inside the FindJsoncpp.cmake file)

VladlenPopolitov commented 1 year ago

Can I give you advice how to solve your issue in the different way, but efficient and with usage of best practice. Your program uses external libraries (dragon, jsoncpp etc). Different versions of your program can use different versions of these libraries and have to run anyway (the users of the program can use different versions and ask for support of this version). Development team also needs exactly binary file of the library, not the file, that generated by themselves or homebrew on their computer. The best practice for external libraries (1) to store them in some place, where you see all libraries for every version of your program. Build all of them, save them in the folder, point linker (cmake) to this folder. As soon as one library changed, update it , copy to new folder, make new version of cmake file pointing to this folder. (2) This storage of the external libraries should not be stored in the repository, it eats the repository space very much without any help. Shared drive for team is enough. In this case you have full control of all external libraries. Also you can build library from any commit, not only from tagged version (last commits could fix some bugs affecting your program). The same approach can be used with include files from external libraries. You need only to include into your make file command adding libraries (target_link_directories) and includes (target_include_directories) for every external library . It is also simplify distribution of the program, as all dependency will be in one place or at least under control.

Build of external libraries usually quite simple:

git clone ...urlfromgithub
cd urlfromgithub
mkdir build
cd build
cmake ..
make
ErikTheBerik commented 1 year ago

That's some nice advice, I just like the idea of automatically downloading and installing external dependencies using cmake. That way I, as the developer, can chose which version of each dependency will be used. Also it makes the installation easier (albeit kinda slower but that is alright).

I will think about doing it the way you mentioned, giving the responsibility of getting the external libraries to whoever uses my project. For now I'll keep the issue open to see if someone has something else to say.

rbugajewski commented 1 year ago

Apple & GCC is a rather uncommon combination these days.

I just wonder what you’re up to, and if you couldn’t use a Linux VM with GCC installed with macOS as the host?

Zerpico commented 6 months ago

@ErikTheBerik did you find solution?

ErikTheBerik commented 6 months ago

Hey @Zerpico I did manage to fix it but I don't really remember what I did. Looking at the current files I can say that I changed the version of drogon to 1.8.6 (which I doubt had anything to do with the fix) and I removed "include(ExternalJsonCpp)" from my cmake. I also remember deleting some old jsoncpp library files I had in my computer which were confusing CMake and then reinstalled jsoncpp via homebrew.

Sadly I don't remember which files I deleted exactly or what other steps I took. Hopefully this message helps you.