gpt4 book ai didi

c++ - 链接器错误 : Undefined reference C++

转载 作者:行者123 更新时间:2023-11-28 01:47:03 26 4
gpt4 key购买 nike

我有这个 undefined reference 错误,我不知道它来自哪里:

/usr/local/clion-2017.1.1/bin/cmake/bin/cmake --build /home/jscherman/CLionProjects/algo3-tp3-cmf/cmake-build-debug --target experimentos -- -j 4
Scanning dependencies of target experimentos
[ 50%] Building CXX object CMakeFiles/experimentos.dir/experimentos.cpp.o
[100%] Linking CXX executable experimentos
CMakeFiles/experimentos.dir/experimentos.cpp.o: In function `main':
/home/jscherman/CLionProjects/algo3-tp3-cmf/experimentos.cpp:12: undefined reference to `cmfExacto(int, int, std::__cxx11::list<Eje, std::allocator<Eje> >&)'
/home/jscherman/CLionProjects/algo3-tp3-cmf/experimentos.cpp:13: undefined reference to `heuristicaConstructiva(int, std::__cxx11::list<Eje, std::allocator<Eje> >)'
collect2: error: ld returned 1 exit status
CMakeFiles/experimentos.dir/build.make:94: recipe for target 'experimentos' failed
make[3]: *** [experimentos] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/experimentos.dir/all' failed
make[2]: *** [CMakeFiles/experimentos.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/experimentos.dir/rule' failed
make[1]: *** [CMakeFiles/experimentos.dir/rule] Error 2
Makefile:118: recipe for target 'experimentos' failed
make: *** [experimentos] Error 2

experimentos.cpp(目标)

#include "cmf-algo-exacto.h"
#include "cmf-heuristica-constructiva-golosa.h"

int main(int argc, char** argv) {
int n = 5, m = 10;
std::list<Eje> grafo = Utils::generarGrafo(n, m, false, 0, 0);
std::cout << "Exacto: " << cmfExacto(n, m, grafo) << std::endl;
std::cout << "Constructiva: " << heuristicaConstructiva(n, grafo) << std::endl;
return 0;
}

cmf-heuristica-constructiva-golosa.h

#ifndef TEST_DEBUGGER_CMF_HEURISTICA_CONSTRUCTIVA_GOLOSA_H
#define TEST_DEBUGGER_CMF_HEURISTICA_CONSTRUCTIVA_GOLOSA_H

#include <iostream>
#include "Clique.h"
#include "Eje.h"
#include "DisjointSet.h"
#include <list>
#include "stringTokenizer.hpp"
#include "Utils.h"
#include <fstream>

Clique heuristicaConstructiva(int n, std::list<Eje> &listaIncidencias);
Clique hconstructiva(int n, std::list<int> *listaAdyacencias);

#endif //TEST_DEBUGGER_CMF_HEURISTICA_CONSTRUCTIVA_GOLOSA_H

cmf-heuristica-constructiva.cpp

#include "cmf-heuristica-constructiva-golosa.h"

Clique hconstructiva(int n, std::list<int> *listaAdyacencias){
...
}

Clique heuristicaConstructiva(int n, std::list<Eje> listaIncidencias) {
...
}

int main(int argc, char** argv) {
...
return 0;
}

cmf-algo-exacto.h

#ifndef TEST_DEBUGGER_CMF_ALGO_EXACTO_H
#define TEST_DEBUGGER_CMF_ALGO_EXACTO_H

#include <iostream>
#include "Clique.h"
#include "Eje.h"
#include "DisjointSet.h"
#include <list>
#include "stringTokenizer.hpp"
#include "Utils.h"
#include <fstream>

Clique * cmfExacto(DisjointSet &uds, std::list<Eje> ejesNoAgregados, list<int> *listaAdyacencias);

Clique * cmfExacto(int n, int m, std::list<Eje> &listaIncidencias);

#endif //TEST_DEBUGGER_CMF_ALGO_EXACTO_H

cmf-算法-exacto.cpp

#include "cmf-algo-exacto.h"

Clique * cmfExacto(int n, int m, std::list<Eje> &listaIncidencias) {
...
}

Clique * cmfExacto(DisjointSet &uds, std::list<Eje> ejesNoAgregados, list<int> *listaAdyacencias){
...
}

int main(int argc, char** argv) {
...
return 0;
}

所以,据我所知,编译器大喊它没有找到那些 cmfExactoheuristicaConstructiva 函数,但我看不到问题所在。这里有什么问题?

最佳答案

你确定你真的编译了所有的cpp文件吗?看起来您的 CMake/编译器不包括 cmf-algo-exacto.cppcmf-heuristica-constructiva.cpp

此外,对于这些文件,您可能会遇到错误,因为您多次定义了 main 函数。最好的方法可能是创建一个单独的 main.cpp 文件并将(唯一的)main 函数放在那里。

编辑:Igor Tandetnik 是对的,参数不匹配

关于c++ - 链接器错误 : Undefined reference C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com