简体   繁体   中英

clang: error: linker command failed with exit code 1 (use -v to see invocation) console app

this code should work but I am getting a linker error. I don't know where to look to fix this. I keep seeing things about cocoa pods and I don't have cocoa pods.

//main.cpp
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include "ItemToPurchase.cpp"
    #include "ItemToPurchase.hpp"

int main(int argc, const char * argv[]) {
    // insert code here...
    // create objects
    ItemToPurchase item1;
    //ItemToPurchase item2;

    std::cout << "Item1" << std::endl;
    item1.setName();
    //item1.SetPrice();
    //item1.SetQuantity();


    std::cout << "Item2" << std::endl;
    //item2.setName();
    //item2.SetPrice();
    //item2.SetQuantity();

    //test

    std::cout << std::endl << std::endl;
    //item1.getName();
    //item1.GetPrice();
    //item1.GetQuantity();



    return 0;
}

here is the ItemToPurchase class cpp file

//ItemToPurchase
#include "ItemToPurchase.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>



std::string ItemToPurchase::setName(){
    std::cout << "Enter the item name: " << std::endl;
    std::cin >> ItemToPurchase::ItemName;
    return ItemToPurchase::ItemName;

}
/*
std::string ItemToPurchase::getName() {
    return ItemToPurchase::ItemName;
}

int ItemToPurchase::SetPrice(){
    std::cout << "Enter the item price: " << std::endl;
    std::cin >> ItemToPurchase::ItemPrice;
    return ItemToPurchase::ItemPrice;

}

int ItemToPurchase::GetPrice(){
    return ItemToPurchase::ItemPrice;

}


int ItemToPurchase::SetQuantity() {
    std::cout << "Enter the Quantity: " << std::endl;
    std::cin >> ItemToPurchase::ItemQuantity;
    return 0;
}

int ItemToPurchase::GetQuantity() {
    return ItemToPurchase::ItemQuantity;
}
    */

and here is the header file for that cpp file it just has the class declaration.

#ifndef ItemToPurchase_hpp
#define ItemToPurchase_hpp

#include <stdio.h>
#include <stdlib.h>
#include <string>


class ItemToPurchase {
public:

    std::string setName();
    std::string getName();
    int SetPrice();
    //int GetPrice();
    //int GetQuantity();
    //int SetQuantity();
    //ItemToPurchase();

private:
    std::string ItemName = "none";
    int ItemPrice = 0;
    int ItemQuantity = 0;


} items;









#endif /* ItemToPurchase_hpp */

and this is the full error

duplicate symbol __ZN14ItemToPurchase7setNameEv in:
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/ItemToPurchase.o
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/main.o
duplicate symbol _items in:
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/ItemToPurchase.o
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/main.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If main.cpp and ItemToPurchase.cpp are compiled apart, then definition of setName method is duplicated, because first definition is in main.cpp (line #include "ItemToPurchase.cpp" included content of source file where definition of setName is) and the second definition is in ItemToPurchase.cpp . So to resolve this you should remove #include "ItemToPurchase.cpp" from main.cpp file.

Second problem, you cannot define variables in header file, look at definition of ItemToPurchase class

class ItemToPurchase {
 public:
   //...
} items; // you have defined items variables in header

you should delete items .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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