Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Bassemaed

macrumors newbie
Original poster
May 3, 2013
1
0
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 ??:(
 
Last edited by a moderator:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
I got the same error when I tried but taking out the keyword inline made it compile and run just fine.
 

chickenwingfly

macrumors 6502a
Mar 8, 2012
633
22
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

macrumors G3
Nov 26, 2007
9,561
6,059
If you declare it separately it works. I found something here 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:

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

chickenwingfly

macrumors 6502a
Mar 8, 2012
633
22
... 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

Suspended
Nov 25, 2005
17,980
5,565
... 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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.