Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode linker command failed with exit code 1 c++

Tags:

c++

xcode

I've written a simple series of functions. When I try to call the last function I get the "linker command" error. The syntax is correct but my program won't compile. Am I missing something or is this an IDE issue?

    #include <iostream>
    #include <cstdlib>
    #include <ctime> 
    #include <time.h>

    using namespace std;


    // Function Prototypes
    int   numGen   ();
    int   questSol ();
    int   questAns ();


int main() {

// Store values of functions in variables
    int ans = questAns();
    int sol = questSol();


    if (ans == sol){
        cout << "Very good! Press Y to continue" << endl;
        questAns();
    } else {
        cout << "Incorrect. Please try again" << endl;
        cin >> ans;
        if(ans == sol){
            questAns();
        }
    }


    return 0;

};

//Generates two random numbers between zero and ten and returns those numbers
int numGen () {

    srand(time(0));
    int one = rand() % 10;
    int two = rand() % 10;

    return one;
    return two;
};

//Takes in the random numbers, multiplies them, and returns that result


int questSol (int one, int two) {


    int solution = one * two;


    return solution;
}


//Takes in random numbers, displays them in cout statement as question, receives and returns user answer to
//question


int questAns (int one, int two) {

    int answer;

    cout << "How much is " << one << " times " << two << "? \n";
    cin >> answer;


    return answer;
}
like image 751
ace7 Avatar asked Oct 26 '15 04:10

ace7


People also ask

How do I fix linker command failed with exit code 1?

Just go to Select Project -> Build Settings -> Search for Enable Bitcode -> If it is selected to Yes, select No. That solved this problem for me. Show activity on this post. In my case the reason of the error is library which was linked two times.

What is clang error linker command failed with exit code 1?

My iOS builds fail with "clang: error: linker command failed with exit code 1" Clang! The error could be caused by App Center building with the xcodeproj instead of the workspace. When adding CocoaPods to your project, you must switch to using the workspace when building in App Center.

What is linker command in C++?

In a C++ project, the linking step is performed after the compiler has compiled the source code into object files (*. obj). The linker (link.exe) combines the object files into a single executable file. Linker options can be set inside or outside of Visual Studio.

What is a clang error in C?

First you get the error because the compiler can not find the definition of the power function that you are using. Even if you write int power(int m, int n); There is an error because you are not defining the function.


1 Answers

You forward declare a function:

int   questAns ();

And then later define a function with a signature:

int questAns (int one, int two);

In C++, functions can have the same name but have different parameters (overloaded functions), so you've never actually defined the questAns that you forward declare and then try to call.

Note: You have the same problem with questSol.

It looks like you don't quite understand the scope of local variables.

Inside numGen you define two ints, one and two. Variables defined within a block (curly braces: {}) exist only within that block. They are local to it. The identifier is only valid within the inner-most block it's defined in, and once you exit it that memory is freed. Returning two ints like you're trying is also impossible.

It looks like you're expecting those ints to be available to your other two functions.

The smallest change you could make is to make int one and two global variables. This means you define them outside of any block (usually at the very top of your code). Then remove the parameter lists from your function definitions, because all the functions can see the global variables. That's generally considered bad programming practice because in more complex programs globals wreak havoc on your code, but in this simple program it'd work and give you a chance to practice understanding variable scope.

Another solution, more in line with what you were trying, is to define an ARRAY of two ints, and return that. Then pass that array to the other two functions. That'd be a better way to do it, and give you a chance to learn about arrays.

like image 186
mock_blatt Avatar answered Sep 24 '22 01:09

mock_blatt