samedi 27 août 2016

Getting error "linker command failed with exit code 1" when creating a project with multiple files

I am a beginner to C++, and following the tutorials on learncpp.com

As on http://ift.tt/146Teyy, I am trying to add another file called add.cpp to my project.

Here is my source code for main.cpp:

#include <iostream>

using namespace std;

int add(int x, int y);

int main() {

    cout << add(5,10) << endl;

}

And for add.cpp

#include <iostream>

using namespace std;

int add(int x, int y) {
    return x+y;
}

I am currently using CLion, student license version with clang compiler on OSX El Capitan.

Here is also the CMakeLists.txt file (auto-generated):

cmake_minimum_required(VERSION 3.3)
project(LearningCPP)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp add.cpp)
add_executable(LearningCPP ${SOURCE_FILES})

However, upon running this project I get this confusing error:

duplicate symbol __Z3addii in:
    CMakeFiles/http://ift.tt/2bWie1K
    CMakeFiles/http://ift.tt/2bHEcIN
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [LearningCPP] Error 1
make[2]: *** [CMakeFiles/LearningCPP.dir/all] Error 2
make[1]: *** [CMakeFiles/LearningCPP.dir/rule] Error 2
make: *** [LearningCPP] Error 2

Any ideas why?? :/

I tried the same code in Code::Blocks IDE and got the same error "linker command failed with exit code 1"

Aucun commentaire:

Enregistrer un commentaire