csv-parser
csv-parser copied to clipboard
How do I include <csv.hpp> in my program when building using CMake?
I've been trying to figure out how to use this csv parser with CMake.
I got it to work fine just using a normal Makefile but when I try compiling with CMake, I get multiple errors.
I first tried to simply include the "csv.hpp" file as a single header file, which led to the following error:
which made my program unusable.
I then tried to compile it as the github page suggested, including # add_subdirectory(csv-parser)
in my CMakeLists.txt file and also including csv
in my target_link_libraries (after my target program).
The csv library compiled fine, apart from a few warnings (which did not prevent it from compiling). However, I tried multiple combinations of #include <csv.hpp>
and none seemed to work! How do I include the library in my code?
I tried the following combinations:
#include <csv.hpp> #incldue <csv/csv.hpp> #include <csv-parser/csv.hpp> #include "csv.hpp" #include "csv/csv.hpp" #include "csv-parser/csv.hpp" #include "csv-parser/include/csv.hpp"
None of them worked! Could you please assist me with this?
Based on your error messages, you have an ancient compiler. I suggest upgrading it.
Thank you so much for replying!
I'll try and upgrade my compiler.
Apart from that, how may I include the library in my code?
I am having the same issue. Tested with CMake 3.16.3 and GCC 9.3.0.
CMakeLists.txt
:
project(Test)
add_subdirectory(csv-parser)
add_executable(test test.cpp)
target_link_libraries(test csv)
test.cpp
:
#include <csv.hpp>
int main() { return 0; }
Error message:
FAILED: CMakeFiles/test.dir/test.o
/usr/bin/c++ -g -MD -MT CMakeFiles/test.dir/test.o -MF CMakeFiles/test.dir/test.o.d -o CMakeFiles/test.dir/test.o -c ../test.cpp
../test.cpp:1:10: fatal error: csv.hpp: No such file or directory
1 | #include <csv.hpp>
| ^~~~~~~~~
I think the issue is that the include directories of the csv
CMake target are not propagating to their dependants. Including the following CMake code:
include(CMakePrintHelpers)
cmake_print_properties(
TARGETS csv
PROPERTIES
INCLUDE_DIRECTORIES
INTERFACE_INCLUDE_DIRECTORIES
)
Gives the following output:
Properties for TARGET csv:
csv.INCLUDE_DIRECTORIES = "/home/robert/test/csv-parser/include"
csv.INTERFACE_INCLUDE_DIRECTORIES = <NOTFOUND>
If we change include_directories(${CSV_INCLUDE_DIR})
in csv-parser/CMakeList.txt
to target_include_directories(csv PUBLIC ${CSV_INCLUDE_DIR})
in csv-parser/include/internal/CMakeList.txt
we get the following result instead:
Properties for TARGET csv:
csv.INCLUDE_DIRECTORIES = "/home/robert/test/csv-parser/include"
csv.INTERFACE_INCLUDE_DIRECTORIES = "/home/robert/test/csv-parser/include/"
and the test example successfully includes <csv.hpp>
. (Although it fails to compile because the C++17 requirement does not propagate to the dependent target either.)
Same issue here, the fix seems pretty simple, is there any plan to have it included in the repository anytime soon?
I also have the same problem. For now I fixed it by setting target_include_directories(csv PUBLIC libs/csv-parser/include)
in the master project. However, it would be ideal to have it directly in the library CMakeLists.txt
. Should we make a pull request?
Many thanks for the amazing library.
Same problem here. @gdmsl thanks for sharing your solution! This also worked for me:
CMakeLists.txt
:
set(DEPENDENCIES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)
set(CSV_INCLUDE_DIR ${DEPENDENCIES_DIR}/csv-parser/include)
include_directories(${CSV_INCLUDE_DIR})
The problem is the single_header folder and issue #139
This will allow you to use FetchContent
# Install a header-only library from github as dependency.
FetchContent_Declare(
csv
GIT_REPOSITORY https://github.com/vincentlaucsb/csv-parser.git
GIT_TAG master
)
FetchContent_MakeAvailable(csv)
include_directories(
${CMAKE_SOURCE_DIR}/${FolderSource} # Find headers from source folder
${csv_SOURCE_DIR}/single_include # Issue #139 for this repo
)
# ... after you add_executable
# Link 3rd party dependencies.
target_link_libraries(${PROJECT_NAME}
csv
)
The problem is the single_header folder and issue #139
This will allow you to use FetchContent
# Install a header-only library from github as dependency. FetchContent_Declare( csv GIT_REPOSITORY https://github.com/vincentlaucsb/csv-parser.git GIT_TAG master ) FetchContent_MakeAvailable(csv) include_directories( ${CMAKE_SOURCE_DIR}/${FolderSource} # Find headers from source folder ${csv_SOURCE_DIR}/single_include # Issue #139 for this repo ) # ... after you add_executable # Link 3rd party dependencies. target_link_libraries(${PROJECT_NAME} csv )
It worked for me!
Had to add include(FetchContent)
.
Full `cmake` file
cmake_minimum_required(VERSION 3.16)
project(fuzz-csv-parser)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
##################################################
# From: https://github.com/vincentlaucsb/csv-parser/issues/135#issuecomment-977528169
##################################################
# Install a header-only library from github as dependency.
FetchContent_Declare(
csv
GIT_REPOSITORY https://github.com/vincentlaucsb/csv-parser.git
GIT_TAG master
)
FetchContent_MakeAvailable(csv)
include_directories(
${CMAKE_SOURCE_DIR}/${FolderSource} # Find headers from source folder
${csv_SOURCE_DIR}/single_include # Issue #139 for this repo
)
##################################################
# End from
##################################################
add_executable(fuzz_csv)
target_sources(fuzz_csv
PRIVATE
test.cpp
)
target_link_libraries(fuzz_csv csv)