我的代码在 CodeLite 中按预期编译和运行。然后我尝试在 Clion 中运行相同的代码,但它给了我这个错误:
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
Run Code Online (Sandbox Code Playgroud)
我试图在线搜索此错误,但无法根据我发现的内容解决它。这是我的代码:
主程序
#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;
}
Run Code Online (Sandbox Code Playgroud)
完整矩阵.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;
}
}
Run Code Online (Sandbox Code Playgroud)
完整矩阵.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
Run Code Online (Sandbox Code Playgroud)
我对 CMake 很陌生,这就是 CMakeLists.txt 中的内容:
cmake_minimum_required(VERSION 3.10)
project(leastSquareFitting)
set(CMAKE_CXX_STANDARD 11)
add_executable(leastSquareFitting main.cpp)
Run Code Online (Sandbox Code Playgroud)
谢谢!任何帮助表示赞赏!
将 CMakeLists 的最后一行更改为:
add_executable(leastSquareFitting main.cpp fullMatrix.cpp)
Run Code Online (Sandbox Code Playgroud)
链接器告诉您的是它找不到包含函数的编译文件,printMatrix如果您查看 cmake 的输出,您会看到 cmake 仅编译 main.cpp 但没有提及 fullMatrix.cpp:
Building CXX object CMakeFiles/leastSquareFitting.dir/main.cpp.o
Run Code Online (Sandbox Code Playgroud)
它没有提到另一个文件的原因是您没有告诉它该文件也是您的来源的一部分。
| 归档时间: |
|
| 查看次数: |
8324 次 |
| 最近记录: |