0

I want to define a function in a separate C++ file. The function takes in array as an argument.

These are my files.

selectionsort.cpp

#include "selectionsort.hpp"

int selectionsort(int a[]){
    
    int length{};
    length = std::size(a);
    
    for(int i{0}; i < length; ++i){
        int smallestIndex{i};
        
        for(int j{i+1}; j < length; ++j){
            if(a[j] < a[smallestIndex]){
                smallestIndex = j;
            };
        };
    std::swap(a[smallestIndex], a[i]);
    };
    return 0;
};

selectionsort.hpp

#ifndef selectionsort_hpp
#define selectionsort_hpp

int selectionsort(int []);

#endif /* selectionsort_hpp */

main.cpp

#include "io.hpp"
#include "monsters.hpp"
#include "selectionsort.hpp"
#include <iostream>
#include <iterator>

int main(){
    
    int a[]{ -1, -100, 0, 10, 100, -2, 2, 10000, 45, -10000};
    selectionsort(a);
    
    std::cout << a[0] << '\n';
    std::cout << a[1] << '\n';
    
    
    return 0;
};

Xcode shows the following error when I run the program.

Undefined symbols for architecture x86_64: "selectionsort(int*)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Undefined symbol: selectionsort(int*)

However if I put the function definition of selectionsort.cpp inside the main.cpp file, everything works perfectly. I dont understand what is the problem here.

robot9
  • 43
  • 4
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Stephen Newell Mar 11 '21 at 04:45
  • In xcode, try select selectionsort.cpp, open the right panel (Cmd + Option + 0), and check the Target Membership checkbox. It may have been unchecked when adding the cpp file, leading to it not being compiled. – TrebledJ Mar 11 '21 at 06:41
  • 1
    @TrebledJ solved it, thanks – robot9 Mar 11 '21 at 23:50

1 Answers1

0

Your .cpp file contains an error that should not compile. As such, I suspect you are not building it in your current setup at all. That would explain why the object code is not being linked into your application, and the linker is unsatisfied.

If you do separate compilation, i.e.

g++ a.cpp -c
g++ b.cpp -c

Now you have two object files, a.o and b.o. To produce a binary, you must link them together:

g++ a.o b.o -o myprogram

In your code you are trying to pass an array:

int selectionsort(int a[]){
    int length{};
    length = std::size(a);
    ...    

And you simply can't do that. The array decays to a pointer when passed to a function, and you can't call std::size on a pointer. This won't compile, because it has lost array information about the argument (which was encoded in its array type) and cannot determine its size when it is just a pointer. It has NO IDEA how big your array is when inside the function. The only way you could make this work is to change the code and pass in the size of the array along with your array. This is a common need, so there are span classes out there, and one was added to c++20, which basically bundles a pointer and a size together and you might consider using one of those.

If you fix your build system to build all of your code, then you fix the c++ error above (including changing your header declaration to match), then you link all your object code together, that may fix the issue you're seeing.

Chris Uzdavinis
  • 6,022
  • 9
  • 16
  • Thanks Chris, while my problem was not having selected the Target Membership check box as suggested by @TrebledJ, your suggestion about array decaying into pointer was helpful. – robot9 Mar 11 '21 at 23:47