包括我在cion项目中的itensor库

时间:2017-04-13 16:15:59

标签: cmake clion

所以我对CLion和CMake很新,所以提前抱歉错误地使用了术语。我遇到了以下问题:

在我的项目中,我希望包含ITensor library,它本质上是一个非CMake项目。我将git克隆到我的计算机上并构建了ITensor项目。接下来我想在另一个与CMake链接的项目中使用它:

main.cpp 中的我的代码:

#include <iostream>
#include "itensor.h"

int main() {
    std::string some_string = "Hello world";
    return 0;
}

和我的 CMakeLists.txt 看起来像是:

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})

不幸的是,项目&#39;教程&#39;不建立在CLION。同样,CLion无法解析依赖itensor.h。 任何人都知道这是为什么,分别是如何解决它?

2 个答案:

答案 0 :(得分:1)

在尝试了Thomas5631的解决方案之后,编译遇到了与lapack的链接问题。我通过添加一些标志来解决这个问题,但我不确定是否所有标志都是必需的。

我的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}")

动机:在文件夹itensor/project_template中有一个示例程序,它很简单,可以使用make(来自终端)进行编译。汇编的输出显示了标志:

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

答案 1 :(得分:0)

我解决了以下main.cpp的问题:

#include <iostream>
#include "itensor/itensor.h"

int main() {
    std::string some_string = "Hello world";
    return 0;
}

以下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})

ITensor的路径可以是相对的(使用$ {PROJECT_SOURCE_DIR}变量),也可以是我所显示的绝对路径。