Error (mach error)

Discussion in 'Mac Programming' started by Bassemaed, May 3, 2013.

  1. Bassemaed, May 3, 2013
    Last edited by a moderator: May 4, 2013

    macrumors newbie

    Joined:
    May 3, 2013
    #1
    hey guys ..
    i just entered this code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    inline double closed_interval_rand(double x0, double x1)
    {
    return x0 + (x1 - x0) * rand() / ((double) RAND_MAX);
    }
    int main(void)
    {
    int pass;
    srand(time(0));
    for (pass = 0; pass < 10; pass++)
    printf("%g\n",  closed_interval_rand(0, 1));
    return 0;
    }
    in a c file and project
    but i receive 2 errors (mach error)
    like this:

    Code:
    Ld /Users/BASSEMAED/Library/Developer/Xcode/DerivedData/NEW-dyqnvdhafewxjicbueafsrvvfyoc/Build/Products/Debug/NEW normal x86_64
        cd /Users/BASSEMAED/Documents/programming/NEW
        setenv MACOSX_DEPLOYMENT_TARGET 10.8
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/BASSEMAED/Library/Developer/Xcode/DerivedData/NEW-dyqnvdhafewxjicbueafsrvvfyoc/Build/Products/Debug -F/Users/BASSEMAED/Library/Developer/Xcode/DerivedData/NEW-dyqnvdhafewxjicbueafsrvvfyoc/Build/Products/Debug -filelist /Users/BASSEMAED/Library/Developer/Xcode/DerivedData/NEW-dyqnvdhafewxjicbueafsrvvfyoc/Build/Intermediates/NEW.build/Debug/NEW.build/Objects-normal/x86_64/NEW.LinkFileList -mmacosx-version-min=10.8 -o /Users/BASSEMAED/Library/Developer/Xcode/DerivedData/NEW-dyqnvdhafewxjicbueafsrvvfyoc/Build/Products/Debug/NEW
    
    Undefined symbols for architecture x86_64:
      "_closed_interval_rand", 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)
    can anyone help me ??:(
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I got the same error when I tried but taking out the keyword inline made it compile and run just fine.
     
  3. macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #3
    That's your problem. That code is cpp, not plain c.
    Try compiling it with clang++ (or g++, or using your project wizard in whatever IDE you're using).

    If you tried to compile it in verbose mode like it says at the end (e.g clang -v main.c), you'd see it's not linking what it should link so that it could compile without errors.
     
  4. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    If you declare it separately it works. I found something here that seems to be relevant:

    So I decided to just go ahead and declare it before defining it and now it compiles and works with clang:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    double closed_interval_rand(double x0, double x1);
    
    inline double closed_interval_rand(double x0, double x1)
    {
    	return x0 + (x1 - x0) * rand() / ((double) RAND_MAX);
    }
    int main(void)
    {
    	int pass;
    	srand(time(0));
    	for (pass = 0; pass < 10; pass++) {
    		printf("%g\n", closed_interval_rand(0, 1));
    	}
    return 0;
    }
    (I also added brackets around the contents of the for loop, but they didn't change anything other than make the code more readable. Speaking of which, you should use the code tags around blocks of code on MacRumor's forums. Type
    Code:
     to start a block and[/code[ to end it - where the bracket on the end is turned the proper way.[COLOR="#808080"]
    
    ----------
    
    [/COLOR][quote="ipsychedelic, post: 17228144"]That's your problem. That code is cpp, not plain c.
    Try compiling it with clang++ (or g++, or using your project wizard in whatever IDE you're using).
    
    If you tried to compile it in verbose mode like it says at the end (e.g [I]clang -v main.c[/I]), you'd see it's not linking what it should link so that it could compile without errors.[/QUOTE]
    
    ... no, it's plain C. Which part makes you think it's not? C99 includes inline functions... nothing else here suggests it's C++ instead of C...
     
  5. macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #5
    Yeah you're right. I just hadn't touched c (or cpp) in years and I gave it a go with clang/clang++, since I had to mess with it recently for some macports fun.
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    The semantics of inline functions is slightly different in C and C++. In C, if you use "inline" without "static", then the program must provide a non-inline implementation. The compiler can choose to call the non-inline version or the inline version. In this case, the non-inline function was missing, therefore the linker complains. In C++, this code would have been Ok.
     
  7. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
  8. macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #8
  9. macrumors 6502

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #9
    Clang seems to have issues with inlines just declared as inline in C. Add static to the declaration.
     
  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Wrong. As explained before, Clang doesn't have any issues. It compiles this code perfectly fine according to the C standard. The C standard says that a non-static inline function must also have non-inline implementation, which is missing.

    Obviously the OP should decide whether the function should be static or not (if it is used only once or only from within the same file then it should be static); if it is static then with any modern compiler "inline" is really not necessary. It is just a hint, and the compiler will inline the function or not, as it pleases.
     

Share This Page