Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cant figure this C++ compile error

Options
  • 26-04-2010 2:06pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    I am trying to get my first sample C++ skeleton structure in place within the Android NDK, so I am trying to get a simple object defined and created and pass its text to the Java layer in Android via the JNI.

    The JNI is not important at the minute, I believe I am doing something wrong with the structure of my C++ class and header file.

    I have it as follows and get the error as quoted

    //Sample.h
    #ifndef SAMPLE_H
    #define SAMPLE_H
    
    #include <string.h>
    
    class Sample {
    public:
      char * string;
    private:
      int number;
    public:
      // Constructor
      Sample();
      Sample(const char*, int);
      // Destructor
      ~Sample() { delete[] string; }
    
      int getInt();
    };
    
    #endif /*SAMPLE_H*/
    


    //Sample.cpp
    #include <string.h>
    #include "inc/Sample.h"
    
    
    Sample::Sample() {
      string = "Empty";
      number = 56;
    }
    
    //class Sample constructor
    Sample::Sample(const char* n, int a) {
      string = strcpy(new char[strlen(n) + 1 ], n);
      number = a;
    }
    
    Sample::~Sample(){
    
    
    }
    
    
    int Sample::getInt() {
      {
        int a = 3;
        return a;
      }
    }
    
    


    //Test.cpp
    #include "inc/Sample.h"
    
    
    JNIEXPORT void JNICALL Java_com_networks_phone_engine_AudioDataStream_initiliase
      (JNIEnv * env, jobject obj){
    
    
    	Sample sampleObject = Sample("test Text", 10);
    
    
    }
    

    Error:
    In function `Java_com_networks_phone_engine_AudioDataStream_initiliase':
    /cygdrive/c/android-ndk-r3/apps/phone/project/jni/Test.cpp:18:
    undefined reference to `Sample::Sample(char const*, int)'
    collect2: ld returned 1 exit status

    So the problem is caused when I try to create a new Sample object in Test.cpp, however if I include Sample.cpp instead of Sample.h everything compiles and runs fine so I presume its a simple error by me?


Comments

  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    draffodx wrote: »
    So the problem is caused when I try to create a new Sample object in Test.cpp, however if I include Sample.cpp instead of Sample.h everything compiles and runs fine so I presume its a simple error by me?

    What command are you using to compile it?
    Sounds like you're compiling Test.cpp but not Sample.cpp.


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Pygmalion wrote: »
    What command are you using to compile it?
    Sounds like you're compiling Test.cpp but not Sample.cpp.

    Argh cant believe thats what I've been doing wrong all morning!

    The files have to be added to the Android.mk files and I renamed Sample without changing it in the .mk file :o

    Thanks!!


Advertisement