PDA

View Full Version : Error (mach error)




Bassemaed
May 3, 2013, 08:43 PM
hey guys ..
i just entered this 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:

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 ??:(



ArtOfWarfare
May 3, 2013, 11:12 PM
I got the same error when I tried but taking out the keyword inline made it compile and run just fine.

ipsychedelic
May 3, 2013, 11:18 PM
in a c file and project
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.

ArtOfWarfare
May 3, 2013, 11:23 PM
If you declare it separately it works. I found something here (http://www.greenend.org.uk/rjk/tech/inline.html) that seems to be relevant:

A function where at least one declaration mentions inline, but where some declaration doesn't mention inline or does mention extern. There must be a definition in the same translation unit. Stand-alone object code is emitted (just like a normal function) and can be called from other translation units in your program.

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

#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.

----------

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.

... 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...

ipsychedelic
May 4, 2013, 12:12 AM
... 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...
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.

gnasher729
May 4, 2013, 02:30 PM
... 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...

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.

ArtOfWarfare
May 4, 2013, 03:31 PM
I just hadn't touched c (or cpp) in years and I gave it a go with clang/clang++

C++ != cpp

cpp = C preprocessor
C++ = The C programming language. Or post increment C.

http://countminus1.wordpress.com/2013/01/04/what-does-cpp-stand-for/

ipsychedelic
May 4, 2013, 03:44 PM
C++ != cpp

cpp = C preprocessor
C++ = The C programming language. Or post increment C.

http://countminus1.wordpress.com/2013/01/04/what-does-cpp-stand-for/
So that was it all about, no wonder people point and laugh at me on the street.

Madd the Sane
May 5, 2013, 02:09 PM
Clang seems to have issues with inlines just declared as inline in C. Add static to the declaration.

gnasher729
May 5, 2013, 07:18 PM
Clang seems to have issues with inlines just declared as inline in C. Add static to the declaration.

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.