I am trying to compile a simple program to read a HDF5 file. The code compiles correctly with h5c++. However I need a cmakelists.txt for the same
readdata.cpp
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
I tried a cmakelists for it but it didnt work. It gave "not defined errors"
readdata.cpp:(.text+0x1d): undefined reference to `H5::FileAccPropList::DEFAULT'
readdata.cpp:(.text+0x24): undefined reference to `H5::FileCreatPropList::DEFAULT'
readdata.cpp:(.text+0x38): undefined reference to `H5check_version'
readdata.cpp:(.text+0x54): undefined reference to `H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
readdata.cpp:(.text+0x60): undefined reference to `H5::H5File::~H5File()'
CMakelists.txt
cmake_minimum_required(VERSION 3.1.0)
PROJECT (readhdf5)
find_package(HDF5 REQUIRED)
include_directories(${HDF5_INCLUDE_DIRS})
add_executable( readdata readdata.cpp )
target_link_libraries( readdata ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
If i put the HDF5_CXX_LIBRARIES and HDF5_LIBRARIES manually the it works.
target_link_libraries( readdata libhdf5.so libhdf5_cpp.so)
So it is not able to read $HDF5_CXX_LIBRARIES and $HDF5_LIBRARIES.How can I fix this?
The code you attempt to compile depends on the HDF5 C++ bindings, which are not searched for by CMake's HDF5 module by default. Explicitly add the binding to the find_package command:
find_package(HDF5 REQUIRED COMPONENTS C CXX)
Related
I am trying to compile a simple C++ program with CMake, but I am getting a linker error :
[2/2] Linking CXX executable bin/MY_PROGRAM
FAILED: bin/MY_PROGRAM
: && g++ -g CMakeFiles/MY_PROGRAM.dir/src/main.cpp.o -o bin/MY_PROGRAM && :
/usr/bin/ld: CMakeFiles/MY_PROGRAM.dir/src/main.cpp.o: in function `main':
/home/user/Code/root/src/main.cpp:27: undefined reference to `str_toupper(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
I have tried looking at some questions with similar issues but couldn't find what I did wrong. There must be a problem with my directory structure and CMake files. I could change the directory structure to make things easier, but I may be missing something important and I'd like to figure out why. Also, I am new to C++ so I might be doing something wrong in the code itself, but my IDE doesn't find any issue.
My directory structure is :
root
|-- CMakeLists.txt
|-- src
|-- main.cpp
|-- utils
|-- CMakeLists.txt
|-- src
|-- strutils.cpp
|-- include
|-- strutils.h
The top-level CMakeLists.txt is :
// general stuff (standard, etc...)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
set(MY_PROGRAM_SRC src/main.cpp)
add_executable(MY_PROGRAM ${MY_PROGRAM_SRC})
include_directories(utils/include)
link_libraries(MY_LIBRARY)
and utils/CMakeLists.txt :
set(MY_LIBRARY_SRC include/strutils.h src/strutils.cpp)
add_library(MY_LIBRARY STATIC ${MY_LIBRARY_SRC})
// This is to include strutils.h in strutils.cpp
// (but is it needed ? I could just #include <string> in strutils.cpp, I guess)
target_include_directories(MY_LIBRARY PUBLIC include)
Finally, the source files :
// strutils.h
#include <string>
void str_toupper(const std::string &in, std::string &out);
// strutils.cpp
#include <algorithm>
#include <strutils.h>
void str_toupper(const std::string &in, std::string &out) {
std::transform(in.begin(), in.end(), out.begin(), [](char c){return std::toupper(c);});
}
// main.cpp
#include <strutils.h>
#include <cstdlib>
#include <cstdio>
#include <string>
int main(int argc, char *argv[]) {
// ...
std::string arg_in;
for (int i = 0; i < argc; i++) {
str_toupper(std::string(argv[i]), arg_in);
}
}
Does anyone have an idea of what's going on ? Thanks !
The first sentence in the manual link_libraries
Link libraries to all targets added later.
You use this directive after the target MY_PROGRAM is added - the target MY_PROGRAM is added prior link_libraries.
Prefer use target_link_libraries(MY_PROGRAM MY_LIBRARY) - other targets can require different dependencies and it's better not use a global set of dependencies for all targets.
I'm coding a small game and I'm using SDLs libraries for the graphics (with a CLion IDE). I've already downloaded SDL2, SDL2_image and SDL2_ttf. In the code, I include the three libraries and use TTF to make some text:
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
bool foo() {
if(SDL_Init(SDL_INIT_EVERYTHING)) {
return false;
}
if (TTF_Init() == -1){
cerr << "Error ." << endl;
}
TTF_Font* font = TTF_OpenFont("Sans.ttf", 20);
SDL_Color color = {100, 0, 0};
SDL_Surface* text;
...
Also, I've the following makefile linking this libraries:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CLIENT_FILES core/client.h core/client.cpp)
set(CONFIGURATION_FILES configuration/configurationClient.cpp configuration/configurationClient.h)
file(GLOB_RECURSE GAME_FILES "game/*.cpp" "game/*.h")
file(GLOB_RECURSE MENU_FILES "menu/*.cpp" "menu/*.h")
add_executable(client main.cpp ${CLIENT_FILES} ${MENU_FILES} ${CONFIGURATION_FILES} ${GAME_FILES})
include(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
PKG_SEARCH_MODULE(SDL2TTF REQUIRED SDL2_ttf>=2.0.0)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIRS})
target_link_libraries(client common SDLPrimitives ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${SDL2TTF_LIBRARIES})
My problem is that the IDE recognize the SDL_ttf library (it doesn't mark in red as an error the TTF functions), but when I try to compile the code, I got many undefined references.
CMakeFiles/client.dir/menu/menuClientVisual.cpp.o: In function `foo()':
source/client/menu/foo.cpp:137: undefined reference to `TTF_Init'
source/client/menu/foo.cpp:141: undefined reference to `TTF_OpenFont'
source/client/menu/foo.cpp:145: undefined reference to `TTF_RenderText_Solid'
source/client/menu/foo.cpp:152: undefined reference to `TTF_RenderText_Solid'
source/client/menu/foo.cpp:157: undefined reference to `TTF_SetFontStyle'
source/client/menu/foo.cpp:160: undefined reference to `TTF_RenderText_Solid'
Any idea?
try including:
find_library(SDL2TTF_LIBRARIES SDL_ttf)
Good day,
here is my code
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv) {
InitializeMagick(*argv);
Image image;
try {
image.read(argv[1]);
}
catch( Exception &error_ ) {
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
int x = image.columns();
cout<<"your picture's width is "<< x << "px"<<endl;
return 0;
}
I use KDevelop(which uses CMake as builder),
when I try to compile the app, it throws me an error
main.cpp:25: undefined reference to `Magick::Image::columns() const'
Here's what my CMakeLists.txt contains.
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(hello ${SOURCE_FILES})
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
find_package(ImageMagick COMPONENTS Magick++)
include_directories(${ImageMagick_INCLUDE_DIRS})
target_link_libraries(hello ${ImageMagick_LIBRARIES})
I figured out that there're often issues with undefined references when CMakeLists isn't written correctly, but I made it according to this About Magick++, how to write the CMakeLists?
where am I wrong? I can add any information needed.
UPD 1.
version of magick++,
8:6.8.9.9-7ubuntu5.7
system info:
Description: Linux Mint 18.1 Serena
UPD 2.
I just removed parenthesis and when tryed to compile with
size_t x = image.columns;
size_t y = image.rows;
KDevelop threw me
main.cpp:25:22: error: cannot convert ‘Magick::Image::columns’ from type ‘size_t (Magick::Image::)() const {aka long unsigned int (Magick::Image::)() const}’ to type ‘size_t {aka long unsigned int}’
even when
auto x = image.columns;
auto y = image.rows;
it throws
main.cpp:25:20: error: cannot convert ‘Magick::Image::columns’ from
type ‘size_t (Magick::Image::)() const {aka long unsigned int
(Magick::Image::)() const}’ to type ‘long unsigned int
(Magick::Image::*)() const’
what's happening?
P.S. hooray, this is my first question on stackoverflow! :-)
If you are able to compile your program without CMake using g++ main.cpp `Magick++-config --cxxflags --cppflags --ldflags --libs` (but for some reason cannot use ${ImageMagick_LIBRARIES} in CMake), then you can make use of Magick++-config in your CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(hello LANGUAGES CXX)
add_executable(hello main.cpp)
target_compile_features(hello PRIVATE cxx_std_11)
find_package(ImageMagick REQUIRED COMPONENTS Magick++)
target_compile_definitions(hello PRIVATE
MAGICKCORE_QUANTUM_DEPTH=16
MAGICKCORE_HDRI_ENABLE=0
)
target_include_directories(hello PRIVATE ${ImageMagick_INCLUDE_DIRS})
execute_process(COMMAND Magick++-config --ldflags
OUTPUT_VARIABLE ImageMagick_LINK_FLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
target_link_libraries(hello PRIVATE ${ImageMagick_LINK_FLAGS})
Here, execute_process allows us to get the result of Magick++-config --ldflags into a variable, which we can pass as flags to the linker through target_link_libraries.
Also, note how I've used target_compile_features rather than setting the global CMAKE_CXX_FLAGS variable, target_compile_definitions rather than add_definitions and target_include_directories rather than include_directories. It's better to use local (target-based) commands rather than modifying global state, both in programming and in CMake, since they can have unforeseen repercussions down the line -- in the context of CMake, those global commands would have affected nested sub-projects.
ForgottenUbrella 's version which I adapted didn't quite work for me, copying a line in from another project fixed it. Note, I'm using c++20 not 11.
I had the following error:
..... undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
and the line that fixed it:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -pthread")
I've tried to make the code as simple as possible and I'm still getting
undefined reference to `boost::log::v2_mt_posix:: . . . etc.
This is driving me crazy, so I'm posting my code:
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(BoostTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -DBOOST_LOG_DYN_LINK")
set(SOURCE_FILES
Logger.cpp
Logger.h
loggertest.cpp)
set(BOOST_ROOT /usr/local)
find_package(Boost 1.57.0 COMPONENTS log thread system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest ${SOURCE_FILES})
target_link_libraries(BoostTest ${Boost_LIBRARIES})
loggertest.cpp
#include "Logger.h"
using namespace idair;
int main(int argc, char* argv[]) {
Logger log;
log.trivialLogging();
return 0;
}
Logger.h
#ifndef ONEPRINT_LOGGER_H
#define ONEPRINT_LOGGER_H
#include <boost/log/core/core.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/core/null_deleter.hpp>
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sources = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace idair {
enum severity_level
{
normal,
warning,
error,
critical
};
class Logger {
public:
void trivialLogging();
};
}
#endif //ONEPRINT_LOGGER_H
Logger.cpp
#include "Logger.h"
using namespace idair;
void Logger::trivialLogging() {
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}
ERRORS
Scanning dependencies of target BoostTest
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/Logger.cpp.o
[100%] Building CXX object CMakeFiles/BoostTest.dir/loggertest.cpp.o
Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `idair::Logger::trivialLogging()':
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
CMakeFiles/BoostTest.dir/Logger.cpp.o:/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: more undefined references to `boost::log::v2_mt_posix::trivial::logger::get()' follow
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record::reset()':
/usr/local/include/boost/log/core/record.hpp:153: undefined reference to `boost::log::v2_mt_posix::record_view::public_data::destroy(boost::log::v2_mt_posix::record_view::public_data const*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_composite_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex>, boost::log::v2_mt_posix::sources::features<boost::log::v2_mt_posix::sources::severity<boost::log::v2_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:456: undefined reference to `boost::log::v2_mt_posix::core::get_logging_enabled() const'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::~record_pump()':
/usr/local/include/boost/log/sources/record_ostream.hpp:278: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>&, boost::log::v2_mt_posix::record&)':
/usr/local/include/boost/log/sources/record_ostream.hpp:258: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2_mt_posix::record&)'
/usr/local/include/boost/log/sources/record_ostream.hpp:259: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::auto_release::~auto_release()':
/usr/local/include/boost/log/sources/record_ostream.hpp:243: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2_mt_posix::aux::stream_provider<char>::stream_compound*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::sources::aux::severity_level<boost::log::v2_mt_posix::trivial::severity_level>::set_value(boost::log::v2_mt_posix::trivial::severity_level)':
/usr/local/include/boost/log/sources/severity_feature.hpp:135: undefined reference to `boost::log::v2_mt_posix::sources::aux::get_severity_level()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:259: undefined reference to `boost::log::v2_mt_posix::core::open_record(boost::log::v2_mt_posix::attribute_set const&)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::core::push_record(boost::log::v2_mt_posix::record&&)':
/usr/local/include/boost/log/core/core.hpp:308: undefined reference to `boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&)'
collect2: error: ld returned 1 exit status
make[3]: *** [BoostTest] Error 1
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
make: *** [BoostTest] Error 2
It looks like you installed a static version of boost.
The boost::log::v2_mt_posix namespace is implemented in dynamically built versions of boost (see this link). Your application will attempt to link against symbols from this namespace if you define the BOOST_LOG_DYN_LINK macro during compilation.
If you have the statically built version of Boost.Log installed to /usr/local, try removing the -DBOOST_LOG_DYN_LINK from your ${CMAKE_CXX_FLAGS} line, regenerate your build directory and attempt the build again. (Side note: typically you will add compiler definitions with the add_definitions() command, see this link)
Or, reinstall a dynamic version of boost.
I'm running the basic OpenCV example with OpenCV3.0.0 dev:
project(ImageDenoise)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
MESSAGE(${OpenCV_LIBS})
MESSAGE(${OpenCV_INCLUDE_DIRS})
Source code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[] )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
When I import this project into QtCreator, I got the following linking errors when building project:
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
CMakeFiles/ImageDenoise.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x7c): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0xf5): undefined reference to `cv::namedWindow(cv::String const&, int)'
main.cpp:(.text+0x144): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
However when I run cmake from command line and using make, then it works perfectly. What is the reason behind this?
dzung#Cronus:~/kSVD/build$ make
Scanning dependencies of target ImageDenoise
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
[100%] Built target ImageDenoise
dzung#Cronus:~/kSVD/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake ImageDenoise Makefile
dzung#Cronus:~/kSVD/build$ ./ImageDenoise
usage: DisplayImage.out <Image_Path>
I had exactly the same issue. I think this is some sort of a Qt-creator bug.
I solved this by:
Remove everything except *.cpp and CMakeLists.txt in the project
folder.
Before creating anything with Qt-creator do: cmake . && make
Now open existing project in Qt-creator.
Now you can run cmake/compile/run etc just fine.