I am trying to link thrift in CMake so that I can compile my test application.
cmake_minimum_required(VERSION 3.10)
project(TestApp)
set(CMAKE_CXX_STANDARD 11)
add_executable(TestApp main.cpp)
target_link_libraries(TestApp -lthrift --static -pthread )
Here is a simple test application including the thrift headers.
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/stdcxx.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
int main() {
std::cout << "BlaBlaBla" << std::endl;
return 0;
}
When I am trying to compile the snippet shown above I am prompted with a linker error:
/opt/JetBrains/apps/CLion/ch-0/181.5087.36/bin/cmake/bin/cmake --build /home/user/Documents/Projects/TestApp/cmake-build-debug --target TestApp -- -j 2
[ 50%] Linking CXX executable TestApp
/usr/bin/ld: cannot find -lthrift
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/TestApp.dir/build.make:95: TestApp] Error 1
make[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/TestApp.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/TestApp.dir/rule] Error 2
make: *** [Makefile:118: TestApp] Error 2
However, the thrift libraries are definitely installed.
How can I link fix this issue and link them correctly in the CMake file?
The issue got solved by cloning the thrift repository from github https://github.com/apache/thrift
Then I simply followed the build instruction there.
I did run ./bootstrap.sh and ./configure followed by make and then sudo make install.
Now the linker recognizes the -lthrift flag.
Related
I'm trying to compile the simplest kernel module possible, from the The Linux Kernel Module Programming Guide. hello.c:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Makefile:
obj-m = hello.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
Output of make:
make -C /lib/modules/5.14.0/build M=/home/parallels/dev/demo modules
make[1]: Entering directory '/home/parallels/linux-5.14'
CC [M] /home/parallels/dev/demo/hello.o
MODPOST /home/parallels/dev/demo/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /home/parallels/dev/demo/hello.o
make[2]: *** [scripts/Makefile.modpost:150: /home/parallels/dev/demo/Module.symvers] Error 1
make[2]: *** Deleting file '/home/parallels/dev/demo/Module.symvers'
make[1]: *** [Makefile:1766: modules] Error 2
make[1]: Leaving directory '/home/parallels/linux-5.14'
make: *** [Makefile:4: all] Error 2
This is all after I have compiled the linux kernel from source and run make modules_install. I don't really understand that second line of output: make[1]: Entering directory '/home/parallels/linux-5.14', that is the directory in which I downloaded and compiled the linux kernel, I don't see what it has to do with this module I'm trying to compile.
I have seen others ask about this missing MODULE_LICENSE() error, but they are usually asking about much more complicated modules and the answers don't seem relevant to this very simple module. A few of the answers had to do with changing the name of either the source code file or the .o file, but trying either way simply produced errors about no rule to make target with the changed name.
Also, this is all on a vm of Kali linux running linux kernel 5.14.0 which I compiled from source.
You need to add call to MODULE_LICENSE to your source file (hello.c in your case). This is what the error message is talking about. E.g.
MODULE_LICENSE("GPL");
Most modules also call MODULE_AUTHOR:
MODULE_AUTHOR("your-name");
This question already has an answer here:
Undefined Python references in C++ using CMake
(1 answer)
Closed 4 years ago.
I am trying to embedded some python code within C++ for a project. I have been able to run this simple tutorial on Windows and it worked (5.1 Very high Level embedding https://docs.python.org/2/extending/embedding.html)
but I wanted to implement it also on my personal Mac and got the following issue when building my project:
====================[ Build | TestCharacter | Debub ]===========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/michaelstettler/CLionProjects/TestCharacter/cmake-build-debug --target TestCharacter -- -j 4
Scanning dependencies of target TestCharacter
[ 50%] Building CXX object CMakeFiles/TestCharacter.dir/main.cpp.o
[100%] Linking CXX executable TestCharacter
Undefined symbols for architecture x86_64:
"_PyMem_RawFree", referenced from:
_main in main.cpp.o
"_PyRun_SimpleStringFlags", referenced from:
_main in main.cpp.o
"_Py_DecodeLocale", referenced from:
_main in main.cpp.o
"_Py_FinalizeEx", referenced from:
_main in main.cpp.o
"_Py_Initialize", referenced from:
_main in main.cpp.o
"_Py_SetProgramName", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [TestCharacter] Error 1
make[2]: *** [CMakeFiles/TestCharacter.dir/all] Error 2
make[1]: *** [CMakeFiles/TestCharacter.dir/rule] Error 2
make: *** [TestCharacter] Error 2
My code is:
#include <iostream>
#include <string>
#include <Python.h> // modified the CMake to make it findable
using namespace std;
int main(int argc, char *argv[]) {
cout << "Hello, World!" << endl;
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program);
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}
I had to modify my CmakeLists.txt to look for the headers as, at first, I was not able to find the Python.h library.
cmake_minimum_required(VERSION 3.13)
project(TestCharacter)
set(CMAKE_CXX_STANDARD 14)
include_directories(/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/Headers)
add_executable(TestCharacter main.cpp)
It seems that my linker is wrong but I haven't figure out what to do. I haven't been able to tool for the invocation either.
If it may be of any help, I am using Clion.
The issue is in your CMakeLists.txt. You can use the builtin FindPythonLibs function to set required paths and libraries:
find_package(PythonLibs REQUIRED)
add_executable(TestCharacter main.cpp)
target_include_directories(TestCharacter PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(TestCharacter PRIVATE ${PYTHON_LIBRARIES})
Note that no version requirements are hardcoded into this snippet. You can do this upon configuring your build by
cmake -D CMAKE_MODULE_PATH=/usr/local/Cellar/python/3.6.5 path/to/your/project
The correct include paths and linker flags should now be passed to the compiler and linker (which you can verify when building on the command line, e.g. with make VERBOSE=1).
I am currently trying to install OSVR-Core on my Debian 9.1 system. I installed all the prerequisites and followed this tutorial. Now I am stuck at making the project. I created a build-directory and tried running
cmake -DCMAKE_PREFIX_PATH="/usr/local/stow/libfunctionality" ~/src/OSVR-Core
but I get the error, that my binary directory should be different from my source directory. My source directory (with the make-files) should be ~/src/OSVR-Core and since I am in ~/src/OSVR-Core/build, I thought this was enough.
How (and where) do I change the location of the binary directory? I tried the command line option -D CMAKE_BINARY_DIR= "path/to/OSVR-Core/build but that did not work. I read online, that you should not set the Binary directory manually, but that it is created/calculated.
This is the CMakeError.log file:
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_42e59/fast"
/usr/bin/make -f CMakeFiles/cmTC_42e59.dir/build.make CMakeFiles/cmTC_42e59.dir/build
make[1]: Entering directory '/home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_42e59.dir/CheckSymbolExists.c.o
/usr/bin/cc -o CMakeFiles/cmTC_42e59.dir/CheckSymbolExists.c.o -c /home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_42e59
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_42e59.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_42e59.dir/CheckSymbolExists.c.o -o cmTC_42e59 -rdynamic
CMakeFiles/cmTC_42e59.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x1b): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_42e59.dir/build.make:97: recipe for target 'cmTC_42e59' failed
make[1]: *** [cmTC_42e59] Error 1
make[1]: Leaving directory '/home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_42e59/fast' failed
make: *** [cmTC_42e59/fast] Error 2
File /home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Determining if the function pthread_create exists in the pthreads failed with the following output: Change Dir: /home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_f2984/fast"
/usr/bin/make -f CMakeFiles/cmTC_f2984.dir/build.make CMakeFiles/cmTC_f2984.dir/build
make[1]: Entering directory '/home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_f2984.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_f2984.dir/CheckFunctionExists.c.o -c /usr/share/cmake-3.7/Modules/CheckFunctionExists.c
Linking C executable cmTC_f2984
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f2984.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_f2984.dir/CheckFunctionExists.c.o -o cmTC_f2984 -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_f2984.dir/build.make:97: recipe for target 'cmTC_f2984' failed
make[1]: *** [cmTC_f2984] Error 1
make[1]: Leaving directory '/home/lenala/src/OSVR-Core/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_f2984/fast' failed
make: *** [cmTC_f2984/fast] Error 2
Have you used the
cmake .. -DJSONCPP_WITH_CMAKE_PACKAGE=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -DCMAKE_CXX_FLAGS=-fPIC
since that's what the tutorial says to use to build the project and that the CMakaList.txt will handle all the paths.
I managed to get it to work. I reinstalled libfunctionality in my ~/src directory. Then I removed the github repository of OSVR-core and cloned it again. After that it worked, using:
$ cmake .. -DJSONCPP_WITH_CMAKE_PACKAGE=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -DCMAKE_CXX_FLAGS=-fPIC
$ make
I am trying to get OpenSSL to work, but it seems to have a problem with linking. Here is what I did:
I downloaded OpenSSL for Linux from https://www.openssl.org/source/ I tried versions 0.9.8zc, 1.0.0o and 1.0.1j, all with the same result.
I installed each OpenSSL version using ./config, make and sudo make install.
For debugging purposes, I went to /usr/lib/ssl and used sudo chmod -R 777 * to remove any restrictions that could have caused the error.
I created the following program:
main.c:
#include <errno.h>
#include <malloc.h>
#include <resolv.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
int main(void) {
SSL_load_error_strings();
return EXIT_SUCCESS;
}
I created the following makefile in the same directory as my .c file:
makefile:
all: main.o
cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl
main.o: main.c
cc -c -Wall main.c -I/usr/local/ssl/include/ -o main.o
When I run the makefile, I get the following error:
cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl
/usr/local/ssl/lib//libssl.a(ssl_err2.o): In function SSL_load_error_strings':
ssl_err2.c:(.text+0x4): undefined reference toERR_load_crypto_strings'
/usr/local/ssl/lib//libssl.a(ssl_err.o): In function ERR_load_SSL_strings':
ssl_err.c:(.text+0xc): undefined reference toERR_func_error_string'
ssl_err.c:(.text+0x28): undefined reference to ERR_load_strings'
ssl_err.c:(.text+0x3c): undefined reference toERR_load_strings'
collect2: ld returned 1 exit status
make: *** [all] Error 1
What am I doing wrong?
Cheers
Alex
As answered on the maillist by scott_n but for the record here, swap the order to -lssl -lcrypto.
Explanation: for static C libraries in general on nearly all systems, members of library files like libxxx.a are only pulled in by the linker if they define things referenced from translation units already linked i.e. to the left in the command line. OpenSSL libssl has (numerous) references to libcrypto. If you link -lcrypto first, those references haven't been seen, so the libcrypto files aren't linked; then you link -lssl and create the unsatisfied references. In cases of mutual dependency also called recursive dependency you may need to repeat a library like -lcrypto -lssl -lcrypto but OpenSSL has no such "backward" references.
i use ubuntu 14.04 64bit,i got a error message when try to compile LuaGnome when i type the make command,the whole error message is as below:
Your Gtk Version is 2.24.23
Version 2.24.23-linux-amd64 not supported; can't download.
script/make-xml.lua failed. The C file content is:
#undef __OPTIMIZE__
#define G_STDIO_NO_WRAP_ON_UNIX
#include <glib-object.h>
#include <glib.h>
#include <gmodule.h>
#include <glib/gstdio.h>
make[1]: *** [build/linux-amd64/gnome/types.xml] Error 1
make: *** [all] Error 2
does anybody know how to fix it?