Armadillo链接错误:ld:未找到架构x86_64的符号

时间:2018-04-22 21:38:20

标签: c++ cmake linker armadillo

我目前正在尝试使用documentation编译一个简单的C ++测试程序。

此库使用optim,我不断收到以下错误消息

  

架构x86_64的未定义符号:" ddot ",引用   从:         main.cpp.o中的double arma :: blas :: dot(unsigned long long,double const *,double const *)ld:找不到符号   体系结构x86_64 clang:错误:链接器命令失败并带有退出代码   1(使用-v查看调用)

我的程序代码如下所示

#include <iostream>
#include <armadillo>
#include "optim.hpp"

double sphere_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
    double obj_val = arma::dot(vals_inp,vals_inp);
    //
    if (grad_out) {
        *grad_out = 2.0*vals_inp;
    }
    //
    return obj_val;
}

double booth_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
    double x_1 = vals_inp(0);
    double x_2 = vals_inp(1);

    double obj_val = std::pow(x_1 + 2*x_2 - 7.0,2) + std::pow(2*x_1 + x_2 - 5.0,2);
    //
    if (grad_out) {
        (*grad_out)(0) = 2*(x_1 + 2*x_2 - 7.0) + 2*(2*x_1 + x_2 - 5.0)*2;
        (*grad_out)(1) = 2*(x_1 + 2*x_2 - 7.0)*2 + 2*(2*x_1 + x_2 - 5.0);
    }
    //
    return obj_val;
}

int main()
{
    //
    // sphere function

    const int test_dim = 5;

    arma::vec x = arma::ones(test_dim,1); // initial values (1,1,...,1)

    bool success = optim::lbfgs(x,sphere_fn,nullptr);

    if (success) {
        std::cout << "lbfgs: sphere test completed successfully." << std::endl;
    } else {
        std::cout << "lbfgs: sphere test completed unsuccessfully." << std::endl;
    }

    arma::cout << "lbfgs: solution to sphere test:\n" << x << arma::endl;

    //
    // Booth function

    arma::vec x_2 = arma::zeros(2,1); // initial values (0,0)

    bool success_2 = optim::lbfgs(x,booth_fn,nullptr);

    if (success_2) {
        std::cout << "lbfgs: Booth test completed successfully." << std::endl;
    } else {
        std::cout << "lbfgs: Booth test completed unsuccessfully." << std::endl;
    }

    arma::cout << "lbfgs: solution to Booth test:\n" << x_2 << arma::endl;

    return 0;
}

在这个论坛中,一遍又一遍地提出了类似的问题。我阅读了很多帖子,但仍然无法弄清楚如何解决这个问题。

人们说这有助于与-larmadillo建立联系。出于某种原因,这对我没有用。所以这是我的CMAKE脚本

cmake_minimum_required(VERSION 3.6)
project(TESTPROJECT)

set(CMAKE_CXX_STANDARD 14)
# set(CMAKE_CXX_FLAGS "-framework Accelerate")

set(SOURCE_FILES main.cpp)

find_package( ARMADILLO REQUIRED )

add_executable(TESTPROJECT ${SOURCE_FILES})

link_directories(... /usr/local/lib)
include_directories(... ${ARMADILLO_INCLUDE_DIR} /usr/include /usr/local/include/optim)

set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}  ${GCC_ARMADILLO_LINK_FLAG}")
set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_ARMADILLO_LINK_FLAG}")

target_link_libraries(TESTPROJECT ${ARMADILLO_LIBRARIES} optim)

在cmake之后运行make导致我的macOS上面的错误。但是,如果我取消注释set(CMAKE_CXX_FLAGS&#34; -framework Accelerate&#34;),它确实有效。但是后来我得到了上述测试函数的错误结果。

我非常感谢任何帮助。

0 个答案:

没有答案