About Monkey 2 › Forums › Monkey 2 Programming Help › SOLVED: difference C vs C++ for importing simple function
This topic contains 1 reply, has 1 voice, and was last updated by
abakobo
2 years, 2 months ago.
-
AuthorPosts
-
February 13, 2017 at 10:05 pm #7144
I’ve trouble with the difference with c and c++
I’ve copied some C example code from the net to be sure i’m using real C style. http://gribblelab.org/CBootcamp/12_Compiling_linking_Makefile_header_files.html (at the half page)
It works if I put the content of the .c file in a .cpp file.What’s wrong there? I know chipmunk is in C but it’s first .h .c files are so complex that I can’t find what makes it ok to be in the .c format!
(a zip file with all files is attached)I get the following error when importing the .c file:
[/crayon]Monkey123[crayon-5cba8d82855d1543873469 inline="true" ]_1src_2prime_0prime.cpp.o:prime_prime.cpp:(.text+0x101): undefined reference to isPrime(int)The files
prime.monkey2:[/crayon]Monkey12345678910111213141516171819202122[crayon-5cba8d82855d9431943551 inline="true" ]#Import "<std>"#Import "primes.h"'switch above commented lines to get it working or not..'#Import "primes.cpp"#Import "primes.c"Using std..ExternFunction isPrime:int(n:Int)PublicFunction Main()Print "Hello World"Print isPrime(7)Endprimes.h
[/crayon]Monkey123[crayon-5cba8d82855e1195436504 inline="true" ]int isPrime(int n); // returns 0 if n is not prime, 1 if n is primeprimes.c or primes.cpp
[/crayon]Monkey12345678910111213141516[crayon-5cba8d82855e6372177019 inline="true" ]int isPrime(int n) {// returns 0 if not prime, 1 if primeif (n<2) return 0; // first prime number is 2if (n==2) return 1; // ensure 2 is identified as a primeif ((n % 2)==0) return 0; // all even numbers above 2 are not primeint i;for (i=3; i*i < n; i++) { // test divisibility up to sqrt(n)if ((n % i) == 0) {return 0;}}return 1;}I’ve tried with #include “prime.h” in the .c file but same problem..
Attachments:
February 13, 2017 at 10:39 pm #7146Solved:
Adding this at the beginning of files[/crayon]Monkey123[crayon-5cba8d828c569999873376 inline="true" ]#ifdef __cplusplusextern "C" {#endifand this at the end
[/crayon]Monkey123[crayon-5cba8d828c56f944244140 inline="true" ]#ifdef __cplusplus}#endifAnd I can now switch the .c and .cpp files.
Looks like mx2 is cpp by default.. -
AuthorPosts
You must be logged in to reply to this topic.