Clion体系结构x86_64的未定义符号 [英] Clion Undefined symbols for architecture x86_64

查看:276
本文介绍了Clion体系结构x86_64的未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在CodeLite中按预期方式编译和运行。
然后我尝试在Clion中运行相同的代码,但是它给了我这个错误:

My code compiled and ran as expected in CodeLite. Then I tried to run the same code in Clion, but it give me this error:

Scanning dependencies of target leastSquareFitting
[ 50%] Building CXX object CMakeFiles/leastSquareFitting.dir/main.cpp.o
[100%] Linking CXX executable leastSquareFitting
Undefined symbols for architecture x86_64:
  "printMatrix(Matrix)", referenced from:
      _main in main.cpp.o
  "constructMatrix(int, std::__1::vector<double, 
std::__1::allocator<double> >)", 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]: *** [leastSquareFitting] Error 1
make[2]: *** [CMakeFiles/leastSquareFitting.dir/all] Error 2
make[1]: *** [CMakeFiles/leastSquareFitting.dir/rule] Error 2
make: *** [leastSquareFitting] Error 2

我尝试在onlin上搜索此错误e,但根据我的发现无法解决。这是我的代码:

I tried to search this error online, but couldn't solve it based on what I found. Here is my code:

main.cpp

#include "fullMatrix.h"
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<double> value = {1.0,2.0,3.0,4.0};
    Matrix m = constructMatrix(2, value);
    printMatrix(m);
    return 0;
}   

fullMatrix.cpp

fullMatrix.cpp

#include "fullMatrix.h"
#include <iostream>
#include <vector>
#include "cmath"
#include <cstdio>
using namespace std;


Matrix constructMatrix(int size, vector<double> x) {
    //initiate variables of a matrix
    Matrix myMatrix;
    myMatrix.size = size;
    myMatrix.value = x;
    return myMatrix;
}

void printMatrix(Matrix matrix){
    // Loop through rows and columns to display all values
    for (int row = 0; row < matrix.size; row++) {
        for (int col = 0; col < matrix.size; col++) {
            cout <<matrix.value[col+row*matrix.size] << " ";
        }
        cout << endl;
    }
}

fullMatrix.h

fullMatrix.h

#ifndef fullMatrix_h
#define fullMatrix_h
#include <vector>
using namespace std;

struct Matrix
{
    int size;
    vector<double> value;
};
Matrix constructMatrix(int size, vector<double> x);

void printMatrix(Matrix matrix);

#endif

我对CMake很陌生,这就是在CMakeLists.txt中:

I am very new to CMake, and this is what's in CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(leastSquareFitting)

set(CMAKE_CXX_STANDARD 11)

add_executable(leastSquareFitting main.cpp)

谢谢您!

推荐答案

将CMakeLists的最后一行更改为:

Change the last line of your CMakeLists to be:

add_executable(leastSquareFitting main.cpp fullMatrix.cpp)

链接器告诉您的内容是找不到包含功能 printMatrix 的已编译文件,并且如果您查看cmake的输出,您会看到cmake仅在编译main .cpp,但没有提及fullMatrix.cpp:

What the linker is telling you is it cannot find the compiled file that contains the function printMatrix, and if you look at your cmake's output you see cmake is only compiling main.cpp but there is no mention of fullMatrix.cpp:

Building CXX object CMakeFiles/leastSquareFitting.dir/main.cpp.o

不提及其他文件的原因是您没有告诉它该文件是其中的一部分您的来源也是如此。

The reason it doesn't mention the other file is you hadn't told it that file was part of your sources too.

这篇关于Clion体系结构x86_64的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆