So I am struggeling with cmake for some time now. I want to use the xmlrpc-c library from here. So I started a new project with main.cpp and CMakeLists.txt and copied the xmlrpc-c as a subdirectory into my project (since xmlrpc-c is unfortunately not a cmake library):
My code is exactly a example from here and looks like this:
#include <iostream>
#include <string>
#include "xmlrpc-c/include/xmlrpc-c/base.hpp"
#include "xmlrpc-c/include/xmlrpc-c/registry.hpp"
#include "xmlrpc-c/include/xmlrpc-c/server_abyss.hpp"
using namespace std;
class hello : public xmlrpc_c::method
{
public:
void execute(const xmlrpc_c::paramList& params, xmlrpc_c::value* retval)
{
string msg(params.getString(0));
params.verifyEnd(1);
cout << msg << endl;
*retval = xmlrpc_c::value_string("XMLRPC server says hello!");
}
};
int main(int argc, char** argv)
{
xmlrpc_c::registry registry;
registry.addMethod("hello", new hello);
xmlrpc_c::serverAbyss server(xmlrpc_c::serverAbyss::constrOpt().registryP(®istry).portNumber(8080));
server.run();
return 1;
}
CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.16)
project(xmlrpc_c_server C CXX)
add_executable(xmlrpc_c_server main.cpp)
target_link_libraries(xmlrpc_c_server -lxmlrpc++ -lxmlrpc_server++ -lxmlrpc_server_abyss++ -lxmlrpc_util++)
The problem I have is that my build-process fails with a linker-error: as far as I understand is the header file registry.hpp not included correctly. If I comment out the code line registry.addMethod("hello", new hello);, I can compile the program without any errors.
====================[ Build | xmlrpc_c_server | Debug ]=========================
/usr/bin/cmake --build /mnt/c/Users/valentin.ackva/CLionProjects/xmlrp-c-server/cmake-build-debug --target xmlrpc_c_server -- -j 9
Scanning dependencies of target xmlrpc_c_server
[ 50%] Building CXX object CMakeFiles/xmlrpc_c_server.dir/main.cpp.o
[100%] Linking CXX executable xmlrpc_c_server
/usr/bin/ld: CMakeFiles/xmlrpc_c_server.dir/main.cpp.o: in function `main':
/mnt/c/Users/struppel/CLionProjects/xmlrp-c-server/main.cpp:31: undefined reference to `xmlrpc_c::registry::addMethod(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, xmlrpc_c::method*)'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/xmlrpc_c_server.dir/build.make:84: xmlrpc_c_server] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/xmlrpc_c_server.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/xmlrpc_c_server.dir/rule] Error 2
make: *** [Makefile:118: xmlrpc_c_server] Error 2
What is missing?
Since the library you want to use doesn't have a cmake project you need to handle this on your own. I would suggest using add_subdirectory and creating a sub-project, where you build the library (either as shared or static depending on your needs).
In addition you need to point cmake to the location of the headers. Try manually adding what's missing to the add_executable or use include_directories and adjust your #includes accordingly.
You can also use some unofficial cmake version of it like this one. If you are using git you can add the repo as a submodule and integrate the code from that repo into your main project.
Since you are new to CMake maybe it's worth looking at a tutorial instead?
https://cliutils.gitlab.io/modern-cmake/
I think it would help your confusion a lot.
Related
I'm trying to build and run a project using tensorflow lite on my debian 11 intel x86_64 architecture. So far I've followed the official documentation and the official github example.
Here are the steps I've followed:
On ~/Desktop/ I ran git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
mkdir tflite_build & cd ~/Desktop/tflite_build
cmake ../tensorflow_src/tensorflow/lite
cmake --build . I've removed the -J flag regardless of what the docs says because it causes my pc to freeze.
mkdir ~/Desktop/tf_test & cd ~/Desktop/tf_test
Create a CMakeLists.txt and a main.cpp file inside tf_testdirectory.
Put the main code from the minimal example on the github repo provided above then this code in CMake:
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTFLITE_DISABLE_TELEMETRY=1")
set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"/home/user/Desktop/tensorflow_src" ABSOLUTE)
endif()
add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
add_executable(minimal minimal.cc)
target_link_libraries(minimal tensorflow-lite)
Created the folder tf_Test/build and ran cmake .. inside it.
After cmake is completed I run make inside the build directory and I'm getting the following error:
...
[100%] Linking CXX executable minimal
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::impl::Interpreter::ReportTelemetrySettings(char const*)':
interpreter.cc:(.text+0x292f): undefined reference to `tflite::telemetry::TelemetryReportSettings(TfLiteContext*, char const*, TfLiteTelemetryInterpreterSettings const*)'
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(subgraph.cc.o): in function `tflite::Subgraph::Invoke()':
subgraph.cc:(.text+0x41c0): undefined reference to `tflite::telemetry::TelemetryReportEvent(TfLiteContext*, char const*, TfLiteStatus)'
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(subgraph.cc.o): in function `tflite::Subgraph::ModifyGraphWithDelegate(TfLiteDelegate*)':
subgraph.cc:(.text+0x6ad0): undefined reference to `tflite::telemetry::TelemetryReportEvent(TfLiteContext*, char const*, TfLiteStatus)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/minimal.dir/build.make:184: minimal] Error 1
make[1]: *** [CMakeFiles/Makefile2:1408: CMakeFiles/minimal.dir/all] Error 2
make: *** [Makefile:149: all] Error 2
Notice that it's not saying this for all the functions. For example this works std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(filename); without errors.
These tree are causing trouble: tflite::ops::builtin::BuiltinOpResolver resolver; std::unique_ptr<tflite::Interpreter> interpreter; tflite::InterpreterBuilder(*model, resolver)(&interpreter);
Note: I've trimmed some code compare to the github example for the sake of testing it, so only the above 4 lines are present on my main.
Why am I getting this error? I've tried compiling with bazel aswell but I'm getting the same error. What am I missing?
Probably it is missing from the CMakeLists file
Update CMakeLists.txt and add
tensorflow/lite/profiling/telemetry/telemetry.cc and
tensorflow/lite/profiling/telemetry/telemetry.h
to TFLITE_PROFILER_SRCS
It is also worth creating issue on Tensorflow repo for the team
I am trying to develop a program that communicates with a PCSC USB reader using Conan and CMake with the LibLogicalAccess library. I followed the instructions of building and installing the library which seemed to have gone fine. I created a small simple console project with a "main.cpp" file. Following the C++ guide on the wiki of the library I tried to call a function from the library which resulted in a "Undefined reference to function. I know there are a lot of topics covering this but I have read as many as I could but could not seem to find the right solution.
I don't have much experience with Ubuntu/CMake/Conan/C++ so it might as well be a very simple fix.
OS: Kubuntu 18.04
Lang: C++
Related software: LibLogicalAccess
2.2.1,
CMake 3.17.1,
Conan 1.25.0
main.cpp
#include <iostream>
#include <logicalaccess/dynlibrary/librarymanager.hpp>
#include <logicalaccess/readerproviders/readerconfiguration.hpp>
#include <logicalaccess/cards/chip.hpp>
int main()
{
std::cout << "Program started\n";
// Reader configuration object to store reader provider and reader unit selection.
std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(new logicalaccess::ReaderConfiguration());
// Set PCSC ReaderProvider by calling the Library Manager which will load the function from the corresponding plug-in
readerConfig->setReaderProvider(logicalaccess::LibraryManager::getInstance()->getReaderProvider("PCSC"));
std::cout << "after..\n";
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(project)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
set(CMAKE_CXX_FLAGS "-I /usr/include/PCSC")
add_executable(project main.cpp)
target_link_libraries(project PUBLIC CONAN_PKG::LogicalAccess)
When I try to build the program using cmake --build . this is the output:
[100%] Linking CXX executable bin/project
CMakeFiles/project.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:191: recipe for target 'bin/project' failed
make[2]: *** [bin/project] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/project.dir/all' failed
make[1]: *** [CMakeFiles/project.dir/all] Error 2
Makefile:103: recipe for target 'all' failed
make: *** [all] Error 2
The weird part is that the first line of code: std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(...) works fine and the second line of code gives an undefined reference.
I have tried other functions in the same file which give the same result. The file compiles and runs fine when I remove the last "setReaderProvider" line of code. Also tried a lot of different little adjustments regarding the conanfile.txt and CMakeLists.txt.
Your error says:
main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string, std::allocator > const&)'
It occurs because your CMake is using libstdc++11 to link, however, Conan is configured to use libstdc++ due backward compatibility.
You need to update your default libcxx:
conan profile update settings.compiler.libcxx=libstdc++11 default
Please, read this section in Conan docs How to Manage GCC ABI to get more information.
Also, it's explained on step 5 of Getting Started.
Now when building again, you will need that your local packages won't be available, because it's a new package, using different settings, so you will need to install, or build from sources. The link to libstdc++11 is automatically managed by Conan, it passes the definitions to CMake.
Regards!
This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I have the following code that works perfectly with gcc by running the command:
gcc -L ~/Installed/C_LIBS/cmocka/lib -I ~/Installed/C_LIBS/cmocka/include hello.c -lcmocka -o hello
When I try to convert that to a CMakeLists.txt it breaks after running cd build && cmake .. && make with the following error codes:
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/main.c.o
[100%] Linking C executable hello
ld: library not found for -lcmocka
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hello] Error 1
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
make: *** [all] Error 2
I have the code setup like this:
my-proj/
- CMakeLists.txt
- main.c
- build/
Here are my files:
main.c
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
(void) state; /* unused */
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project (hello)
include_directories(
SYSTEM ~/Installed/C_LIBS/cmocka/lib
SYSTEM ~/Installed/C_LIBS/cmocka/include
)
add_executable(hello main.c)
target_link_libraries(hello cmocka)
Can someone please tell me what I am doing wrong here? Also maybe point me in the direction of where to learn CMake better?
The include_directories() command only affects header search paths (stuff that is used through #include). It has no effect on library search paths.
Since you know the full path of the library, you should just be linking directly against said full path:
target_link_libraries(hello ~/Installed/C_LIBS/cmocka/lib/libcmocka.a)
However, this is just patching your CMakeLists.txt. What you should be doing is use CMake's library functions, which will be a lot more flexible:
# Include dir
find_path(MOCKA_INCLUDE_DIR
NAMES cmocka.h
PATHS ~/Installed/C_LIBS/cmocka/include
)
#library itself
find_library(MOCKA_LIBRARY
NAMES cmocka
PATHS ~/Installed/C_LIBS/cmocka/lib
)
target_include_directories(hello PRIVATE ${MOCKA_INCLUDE_DIR})
target_link_libraries(hello ${MOCKA_LIBRARY})
The problem is as follows.
I have installed libarmadillo on my Ubuntu distributive via apt utility having liblapack and libblas previously installed. And I'm trying to use it in my CLion project. What my intentions result in is that I'm receiving some build errors. One of them was solved by adding #define ARMA_DONT_USE_WRAPPER.
I have found the way to build my project in this topic -
Armadillo + BLAS + LAPACK: Linking error?. Though, I can build it only with terminal. I assume that the issue with CLion is CMake configuration.
What is the way for me to alter CMake script so that I refine its built behavior and make it compile my project?
In simple words, how do I make it compile my program with g++ main.cpp -o lab2 -O1 -llapack -lblas.
Code sample:
#define ARMA_DONT_USE_WRAPPER
#define ARMA_USE_BLAS
#define ARMA_USE_LAPACK
#include <armadillo>
#include <iostream>
using namespace arma;
using namespace std;
int main() {
mat A(4, 5);
A.load("matrix.txt");
mat B = resize(A, 4, 4);
cout << norm(A, 2);
cout << B;
return 0;
}
The errors I'm issued:
CMakeFiles/lab2.dir/main.cpp.o: In function `double
arma::blas::asum<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:241: undefined reference
to `dasum_'
CMakeFiles/lab2.dir/main.cpp.o: In function `double
arma::blas::nrm2<double>(unsigned long long, double const*)':
/usr/include/armadillo_bits/wrapper_blas.hpp:273: undefined reference
to `dnrm2_'
collect2: error: ld returned 1 exit status
CMakeFiles/lab2.dir/build.make:94: recipe for target 'lab2' failed
make[3]: *** [lab2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/lab2.dir/all'
failed
make[2]: *** [CMakeFiles/lab2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/lab2.dir/rule'
failed
make[1]: *** [CMakeFiles/lab2.dir/rule] Error 2
Makefile:118: recipe for target 'lab2' failed
make: *** [lab2] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(lab2)
set(CMAKE_CXX_STANDARD 11)
add_executable(lab2 main.cpp)
You first need CMake functions to find the libraries liblapack and libblas.
For both there are already functions in the standard cmake distribution so
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
find_package(Armadillo REQUIRED)
See FindBLAS.cmake and FindLAPACK.cmake in your cmake modules directory for documentation what variables they define.
then add them to your targets, e.g.:
target_link_libraries(lab2 ${LAPACK_LIBRARIES} ${BLAS_LIBARIES} ${ARMADILLO_LIBRARIES})
As #Richard Hodges said in a comment, in the Settings you can set the variables fed to CMake as well as define environment variables.
Something also I like to do is to include a custom CMake script to easily tweak a configuration; for example add in your CMakeLists.txt:
include(local.cmake OPTIONAL)
That way, you can put any CMake configuration you want in local.cmake in your source directory, and it will be parsed. Nothing happens if the file does not exist.
When I run make to compile the Makefile produced by cmake, to compile an hello world example of a Qt5 application, the compilation fails with the following error:
Why is the compilation failing?
(details of what exactly I'm trying to do follow)
I'm under Windows 10, using the Qt5.5 binaries downloaded from Qt official website, mingw-w64 gcc and g++ shipped with WinBuilds, and cmake v3.6 downloaded from the official website and installed with the Windows win64-x64 Installer.
I'm trying to compile the following hello world test file, provided in Qt5's official wiki:
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication app (argc, argv);
return app.exec();
}
The Makefile is built successfully using the command cmake -G "MinGW Makefiles" .. from a folder called build inside the directory containing the .cpp file. The following CMakeLists.txt file (taken from Qt5's cmake wiki page, with the addition of the specification of the CMAKE_PREFIX_PATH variable, which is required as for example discussed in this SO post) was used:
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_C_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-gcc-4.8.3.exe)
SET(CMAKE_CXX_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-g++-4.8.3.exe)
project(testproject)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_PREFIX_PATH "C:/WinBuilds/lib64/cmake")
set(CMAKE_PREFIX_PATH "C:/Qt/5.5/mingw492_32/lib/cmake")
find_package(Qt5Widgets)
add_executable(testfile WIN32 test.cpp)
target_link_libraries(testfile Qt5::Widgets)
(I did not use the cmake shipped with Qt5 as that did not work).
Now, the problem arises when I run make (or more precisely, mingw32-make, again shipped with WinBuilds) on the Makefile produced by cmake.
When I do this, the compilation fails with the following error (same one showed in the screenshot above):
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x35): undefined reference to `__imp__ZN16QCoreApplicationC1ERiPPci'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x3e): undefined reference to `__imp__ZN16QCoreApplication4execEv'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x50): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x67): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\testfile.dir/objects.a(test.cpp.obj): bad reloc address 0xc in section `.xdata'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\testfile.dir\build.make:127: recipe for target 'testfile.exe' failed
mingw32-make[2]: *** [testfile.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/testfile.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/testfile.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Why is the compilation failing?
A similar undefined reference error was reported in this other SO question, but the reason there seemed to be different than the present case.
The problem is you are using 32 bit mingw compiled Qt binaries when you are trying to build a 64 bit Qt application. You need to use 64 bit Qt binaries compiled with mingw for this to work.