简体   繁体   English

链接器错误:未定义参考C ++

[英]Linker error: Undefined reference C++

I'm having this undefined reference error which i don't have no clue where it is coming from: 我有这个不确定的参考错误,我不知道它来自哪里:

/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(target) 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 cmf-启发式构造-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 cmf-启发式构造

#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 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-algo-exacto.cpp cmf-algo-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;
}

So, as i understand the compiler is yelling that it is not finding those cmfExacto and heuristicaConstructiva functions but i can't see the problem. 因此,据我了解,编译器大喊它没有找到这些cmfExactoheuristicaConstructiva函数,但我看不到问题。 What is wrong here? 怎么了

Are you sure you are really compiling all the cpp files? 您确定您确实在编译所有cpp文件吗? Looks like your CMake / compiler isn't including cmf-algo-exacto.cpp and cmf-heuristica-constructiva.cpp . 看起来您的CMake /编译器不包含cmf-algo-exacto.cppcmf-heuristica-constructiva.cpp

Also, with those files, you may get errors since you define the main function multiple times. 另外,对于这些文件,由于多次定义了main函数,可能会出错。 Best approach would probably be to create a separate main.cpp file and put the (only) main function there. 最好的方法可能是创建一个单独的main.cpp文件,并将(仅) main函数放在那里。

EDIT: Igor Tandetnik is right, the parameters don't match 编辑:Igor Tandetnik是正确的,参数不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM