I am trying to convert a rather simple CMakeLists.txt to a qmake project. The project compiles fine but I can't get the output I want (the program files are the same!).
It shouldn't be a big deal, but I have spent the whole day and can't seem to figure out the problem. Any help would be greatly appreciated.
Here's the CMakeLists.txt that I am trying to convert:
cmake_minimum_required (VERSION 2.6)
PROJECT (sdpwrapper)
#set the default path for built executables to the "bin" directory
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
FIND_PACKAGE(OpenCV REQUIRED)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
ADD_LIBRARY(objdettrack
./src/MyPipe.cpp
./src/SDPWrapper.cpp
./src/online_tracker.cpp
./src/BaseObjectTracker2D.cpp)
INCLUDE_DIRECTORIES(include
/usr/include/python2.7
../dep/NOMT/include/
../dep/NOMT/dep/eigen/
../dep/NOMT/dep/libDAI-0.3.2/include/)
TARGET_LINK_LIBRARIES(objdettrack
${PROJECT_SOURCE_DIR}/../dep/NOMT/lib/libcppmodule.so
python2.7
${OpenCV_LIBS})
ADD_EXECUTABLE(example ./src/main.cpp)
TARGET_LINK_LIBRARIES(example objdettrack)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_C_FLAGS}")
ENDIF()
and here's the qmake project file I created:
QT += widgets
TARGET = ./build/DummyProject
#CONFIG += console
#CONFIG -= app_bundle
SOURCES += ./src/main.cpp \
./src/MyPipe.cpp \
./src/online_tracker.cpp \
./src/SDPWrapper.cpp \
./src/BaseObjectTracker2D.cpp
TEMPLATE = app
#TEMPLATE += lib
# ZEESHAN:INTEGRATION CODE
INCLUDEPATH += ./include
INCLUDEPATH += /usr/include/python2.7
INCLUDEPATH += /home/ma/zeeshan/code/Joint/KITTI_Jun2016/slam/app/faster-rcnn/dep/NOMT/include/
INCLUDEPATH += /home/ma/zeeshan/code/Joint/KITTI_Jun2016/slam/app/faster-rcnn/dep/NOMT/dep/eigen/
INCLUDEPATH += /home/ma/zeeshan/code/Joint/KITTI_Jun2016/slam/app/faster-rcnn/dep/NOMT/dep/libDAI-0.3.2/include/
QMAKE_LIBDIR += /home/ma/zeeshan/code/Joint/KITTI_Jun2016/slam/app/faster-rcnn/dep/NOMT/lib
LIBS += -lcppmodule
LIBS += -lpython2.7
LIBS += -lpynomt
QMAKE_LIBDIR += ../../faster-rcnn/cpp/lib
LIBS += -Lobjdettrack
CONFIG += link_pkgconfig
PKGCONFIG += opencv
QMAKE_CXXFLAGS+= -fopenmp
QMAKE_LFLAGS += -fopenmp
Any suggestions would be highly appreciated.
Zeeshan
Related
I wonder how CMake automatically detects that main.cpp depends on header.h
// header.h
int f() {
return 0;
}
// main.cpp
#include "header.h"
int main() {
return f();
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Cppref)
add_executable(main main.cpp)
When I run cmake . -B build it creates the following make target in ./build/CMakeFiles/main.dir/build.make
CMakeFiles/main.dir/main.cpp.o: CMakeFiles/main.dir/compiler_depend.ts
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/nikolay/Cpp/Train/Cppref/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building CXX object CMakeFiles/main.dir/main.cpp.o"
/usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/nikolay/Cpp/Train/Cppref/main.cpp
Pay attention to the -MD compiler option, as
it is used to dump dependencies visible to the preprocessor.
So after the first build it will create ./build/CMakeFiles/main.dir/main.cpp.o.d with the following content
CMakeFiles/main.dir/main.cpp.o: /home/nikolay/Cpp/Train/Cppref/main.cpp \
/home/nikolay/Cpp/Train/Cppref/header.h
So whenever you change header.h, the target main.o will be rebuilt.
I want to use c++ tensorflow API through ndk and cmake, I can create the library but when I load a frozen model a lot of errors like this when I try to load graph:
E/native: op_kernel.cc:1148 OpKernel ('op: "PopulationCount"
device_type: "CPU" constraint { name: "T" allowed_values { list {
type: DT_INT32 } } }') for unknown op: PopulationCount
So I understand that my library does not support the operations.
Do I need to add another library to support these operations?
I based my Cmake script on this Cmakelist and
I used the build_android_all.sh script to build the dependencies for armeabi-v7a.
This is how my cmake script looks like:
cmake_minimum_required(VERSION 3.4.1)
include(ExternalProject)
SET(PROJECT_NAME tf_native_lib)
SET(OpenCV_FOUND true )
SET(PREBUILT_DIR ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/gen)
SET(TARGET_NSYNC_LIB ${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile /downloads/nsync/builds/${ANDROID_ABI}.android.c++11)
FIND_PACKAGE(OpenCV REQUIRED)
add_library( # Sets the name of the library.
${PROJECT_NAME}
SHARED
src/main/cpp/native-lib.h
src/main/cpp/native-lib.cpp
src/main/cpp/TensorflowInferenceHandler.h
src/main/cpp/TensorflowInferenceHandler.cpp
)
add_library(lib_proto STATIC IMPORTED )
set_target_properties(lib_proto PROPERTIES IMPORTED_LOCATION
${PREBUILT_DIR}/protobuf_android/${ANDROID_ABI}/lib/libprotobuf.a)
add_library(lib_nsync STATIC IMPORTED )
set_target_properties(lib_nsync PROPERTIES IMPORTED_LOCATION
${TARGET_NSYNC_LIB}/libnsync.a)
add_library(lib_tf STATIC IMPORTED )
set_target_properties(lib_tf PROPERTIES IMPORTED_LOCATION
${PREBUILT_DIR}/lib/android_${ANDROID_ABI}/libtensorflow-core.a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIS_SLIM_BUILD \
-std=c++11 -fno-rtti -fno-exceptions \
-O2 -Wno-narrowing -fomit-frame-pointer \
-mfpu=neon -mfloat-abi=softfp -fPIE -fPIC \
-ftemplate-depth=900 \
-DGOOGLE_PROTOBUF_NO_RTTI \
-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} \
-Wl,--allow-multiple-definition \
-Wl,--whole-archive \
-fPIE -pie -v")
# MESSAGE("tensorflow lib dir ${TENSORFLOW_LIB_DIR}")
target_link_libraries( # Specifies the target library.
${PROJECT_NAME}
${OpenCV_LIBS}
android
dl
log
m
z
jnigraphics
lib_tf
lib_proto
lib_nsync)
include_directories(
${OPENCV_INCLUDE_DIRS}
${PREBUILT_DIR}/proto
${PREBUILT_DIR}/protobuf_android/${ANDROID_ABI}/include
${PREBUILT_DIR}/nsync/public
${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/downloads/eigen
${TENSORFLOW_ROOT_DIR}/bazel-tensorflow/external/nsync/public
${TENSORFLOW_ROOT_DIR}/bazel-genfiles
${TENSORFLOW_ROOT_DIR}
../../../cpp_utils)
If this is not possible, where are the sources to link the tensorflow_inference.so with my Jni Sources?
Thanks in advance.
Unai.
The PopulationCount and few other Ops are declared in tensorflow/core/ops/bitwise_ops.cc. Add that file to the end of /tensorflow/contrib/makefile/tf_op_files.txt. Then recompile the Tensorflow. That is what worked for me.
so I am very new to CLion and CMake, so sorry in advance for wrong usage of terminology. I am suffering the following problem:
In my project I want to include the ITensor library which is essentially a non-CMake project. I cloned the git to my computer and build the ITensor project. Next I wanted to use it in another project linking against it with CMake:
My Code in main.cpp:
#include <iostream>
#include "itensor.h"
int main() {
std::string some_string = "Hello world";
return 0;
}
and my CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.6)
project(tutorial)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(ITENSOR_DIR PATH/TO/ITENSOR)
include_directories(ITENSOR_DIR/itensor)
set(SOURCE_FILES
main.cpp
${ITENSOR_DIR}/itensor/itensor.h
${ITENSOR_DIR}/itensor/itensor.cc)
add_executable(tutorial ${SOURCE_FILES})
Unfortunately, the project 'tutorial' does not build in CLion. Likewise, CLion cannot resolve the dependency itensor.h.
Anybody an Idea for why this is, respectively how to fix it?
After trying Thomas5631's solution the compilation ran into linking issues with lapack. I solved this by adding some flags, though I'm not sure if all of them are required.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(my_project)
#Bring the headers into the project (full or relative path)
include_directories(itensor)
#Link the Itensor library
add_library(itensor STATIC IMPORTED)
set_property(TARGET itensor PROPERTY IMPORTED_LOCATION /home/david/my_project/itensor/lib/libitensor.a)
#Set a variable with all the new flags
set(ITENSOR_FLAGS "-DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0")
set(ITENSOR_LINK_FLAGS "-DPLATFORM_lapack -L/home/david/my_project/itensor/lib -litensor -lpthread -L/usr/lib -lblas -llapack")
#Append the new flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${ITENSOR_FLAGS}")
add_executable(my_project main.cpp)
target_link_libraries(my_project itensor "${ITENSOR_LINK_FLAGS}")
Motivation: In the folder itensor/project_template there is a sample program which is simple enough to compile with make (from the terminal). The output of the compilation reveals the flags:
g++ -m64 -std=c++11 -c -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable -o myappname.o myappname.cc
[... some warnings ...]
g++ -m64 -std=c++11 -c -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable -o myclass.o myclass.cc
g++ -m64 -std=c++11 -I. -I/home/david/my_project/itensor -I/usr/include -O3 -DNDEBUG -Wall -DPLATFORM_lapack -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -Wno-unused-variable myappname.o myclass.o -o myappname -L/home/david/my_project/itensor/lib -litensor -lpthread -L/usr/lib -lblas -llapack
I got around the issue with the following main.cpp:
#include <iostream>
#include "itensor/itensor.h"
int main() {
std::string some_string = "Hello world";
return 0;
}
And the following CMakeLists.txt:
project(tutorial)
cmake_minimum_required(VERSION 3.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#Bring the headers into the project
include_directories(/home/tom/Documents/workspace/ITensor/)
#Link the library
add_library(itensor STATIC IMPORTED)
set_property(TARGET itensor PROPERTY IMPORTED_LOCATION /home/tom/Documents/workspace/ITensor/lib/libitensor.a)
set(SOURCE_FILES main.cpp)
add_executable(tutorial ${SOURCE_FILES})
Where the path to ITensor can be either relative (using the ${PROJECT_SOURCE_DIR} variable) or absolute as I have shown.
I'm getting the error: lib/libhrlLib.so: undefined reference to `hrlQseqDev::waiting(bool, int)' and some more ...
I'm trying to build my project with cmake (3.7.2) instead of with qmake (Qt5)
CMakeLists.txt:
project(${TARGET_NAME})
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(POSITION_INDEPENDENT_CODE FALSE)
macro(NAMELIST erg in)
set(os "")
set(srcs ${in})
separate_arguments(srcs)
foreach(is ${srcs})
set(os ${os} ${is})
endforeach(is)
set(${erg} ${os})
endmacro(NAMELIST)
macro(FINDMODULES erg in)
set(os "")
set(srcs ${in})
separate_arguments(srcs)
foreach(is ${srcs})
find_package(Qt5${is} REQUIRED)
set(os ${os} Qt5::${is})
endforeach(is)
set(${erg} ${os})
endmacro(FINDMODULES)
NAMELIST(SRCS ${TARGET_SRCS})
FINDMODULES(QLIBS ${QMODULES})
if(INC_PATH)
NAMELIST(INCS ${INC_PATH})
set(INCLUDES ${INCS})
endif(INC_PATH)
if(TARGET_EXTLIBS)
NAMELIST(EXTRA_LIBS ${EXTRA_LIBS} "${TARGET_EXTLIBS}")
endif(TARGET_EXTLIBS)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
include_directories(${INCLUDES})
if(BUILD_LIB)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-shared")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-shared")
add_library(${TARGET_NAME} SHARED ${SRCS})
target_link_libraries(${TARGET_NAME} ${QLIBS} ${EXTRA_LIBS})
set_property(TARGET ${TARGET_NAME} PROPERTY VERSION "1.0.0")
set_property(TARGET ${TARGET_NAME} PROPERTY SOVERSION 1 )
install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION lib)
install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION lib NAMELINK_ONLY)
else()
if(USE_LIB_PATH)
add_executable(${TARGET_NAME} ${SRCS})
target_link_libraries(${TARGET_NAME} -L${OUT_PATH}/lib lib${USE_LIB}.so.1
${QLIBS} ${EXTRA_LIBS})
else()
add_executable(${TARGET_NAME} ${SRCS})
target_link_libraries(${TARGET_NAME} ${QLIBS} ${EXTRA_LIBS})
endif(USE_LIB_PATH)
install(TARGETS ${TARGET_NAME} RUNTIME DESTINATION bin)
endif(BUILD_LIB)
The shared library is built with:
cmake -G "Unix Makefiles" -DINC_PATH:STRING="some includes" -DTARGET_EXTLIBS:STRING="sys libs" -DTARGET_NAME:STRING=LibName -DBUILD_LIB:BOOL=1 -DTARGET_SRCS:STRING="cpp- and c-file" -DQMODULES:STRING="Core Gui Widgets PrintSupport" -DOUT_PATH:STRING=InstallPath .. -DCMAKE_INSTALL_PREFIX=InstallPath >> /dev/null
When I try to build a program against this shared library with:
cmake -G "Unix Makefiles" -DINC_PATH:STRING="some includes" -DTARGET_EXTLIBS:STRING="sys libs" -DTARGET_NAME:STRING=ProgName -DBUILD_LIB:BOOL=0 -DTARGET_SRCS:STRING="cpp-file" -DQMODULES:STRING="Core Gui Widgets PrintSupport" -DOUT_PATH=InstallPath -DUSE_LIB:STRING="LibName" -DUSE_LIB_PATH:STRING="BuildPath of LibName" .. -DCMAKE_INSTALL_PREFIX=InstallPath >> /dev/null
I get the error 'lib/libhrlLib.so: undefined reference to ...'.
I do not get this error when the library was built with qmake.
How can I fix this?
I have a very simple openmp hello world program as this:
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("hello, from %d.\n", id);
}
return 0;
}
I can compile it in my terminal with gcc-5 -fopenmp hello.c -o hello.o
But I want to code in CLion, it uses CMake to organize project, when I just click the run button, I will got an error
fatal error: 'omp.h' file not found
#include <omp.h>
I did search in google, and add something into my CMakeLists.txt file
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.3)
project(openmp)
OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")
set(SOURCE_FILES hello.c omp.c)
add_executable(openmp ${SOURCE_FILES})
Your code is C, not C++, so instead of changing CMAKE_CXX_FLAGS, change CMAKE_C_FLAGS:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
And set your C compiler to gcc-5:
set(CMAKE_C_COMPILER /your/path/to/gcc-5)
To know gcc-5 path, in a terminal, type which gcc-5
See also: How to specify new gcc path for cmake