Home android Why did you get an undefined reference to error?

Why did you get an undefined reference to error? [duplicate]

Author

Date

Category

I wrote a cipher. part in C on Qt. Everything worked there. Now I want to screw it to Java in Android Studio, but such an error appeared, although everything seems to be announced everywhere.

Error # 1. (native-lib.cpp file)

Error # 2

MainAcrivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
 String msg = "Hello from Java";
 // Used to load the 'native-lib' library on application startup.
 static {
      System.loadLibrary ("native-lib");
 }
 @Override
 protected void onCreate (Bundle savedInstanceState) {
  super.onCreate (savedInstanceState);
  setContentView (R.layout.activity_main);
  // Example of a call to a native method
  TextView tv = (TextView) findViewById (R.id.sample_text);
  tv.setText (stringFromJNI (msg));
 }
 / **
  * A native method that is implemented by the 'native-lib' native library,
  * which is packaged with this application.
 * /
 public native String stringFromJNI (String msg);
}

native-lib.cpp

# include & lt; jni.h & gt;
#include & lt; string & gt;
#include "gost34-12-15.h"
#include "gost34-13-15.h"
#include "main.cpp"
std :: string jstring2string (JNIEnv * env, jstring jStr);
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_maxim_onlyforc_MainActivity_stringFromJNI (
    JNIEnv * env,
    jobject obj, jstring msg) {
  jstring msg2 = (env, msg);
  std :: string text = jstring2string (env, msg2);
  return env- & gt; NewStringUTF (text.c_str ());
}
std :: string jstring2string (JNIEnv * env, jstring jStr) {
  if (! jStr)
   return "";
  const jclass stringClass = env- & gt; GetObjectClass (jStr);
  const jmethodID getBytes = env- & gt; GetMethodID (stringClass, "getBytes", "
  (Ljava / lang / String;) [B ");
  const jbyteArray stringJbytes = (jbyteArray) env- & gt; CallObjectMethod (jStr,
  getBytes, env- & gt; NewStringUTF ("UTF-8"));
  size_t length = (size_t) env- & gt; GetArrayLength (stringJbytes);
  jbyte * pBytes = env- & gt; GetByteArrayElements (stringJbytes, NULL);
  std :: string ret = std :: string ((char *) pBytes, length);
  env- & gt; ReleaseByteArrayElements (stringJbytes, pBytes, JNI_ABORT);
  env- & gt; DeleteLocalRef (stringJbytes);
  env- & gt; DeleteLocalRef (stringClass);
  return ret;
}

main.cpp

# include & lt; jni.h & gt;
#include & lt; string & gt;
#include "gost34-12-15.h"
#include "gost34-13-15.h"
unsigned char * jstring2char (JNIEnv * env, jstring);
jstring crypto (JNIEnv * env, jstring msg)
{
  // Clear text for GOST 34.13-15 \\

  byte * text = jstring2char (env, msg);
  byte key [] = {// key
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11,
        0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76,
        0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
  byte IV [] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0, 0xa1, 0xb2, // sync. package
0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67,
         0x78, 0x89, 0x90, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
  init128Gost34_12_15 (key);
  encrypt_decrypt_OFB (IV, text, sizeof (text));
  close128Gost34_12_15 ();
  closeGost34_13_15 (IV);
  jstring newText = (* env) .NewStringUTF ((const char *) text);
  return newText;
}
unsigned char * jstring2char (JNIEnv * env, jstring jStr) {
  const jclass stringClass = env- & gt; GetObjectClass (jStr);
  const jmethodID getBytes = env- & gt; GetMethodID (stringClass, "getBytes", "(Ljava / lang / String;) [B");
  const jbyteArray stringJbytes = (jbyteArray) env- & gt; CallObjectMethod (jStr, getBytes, env- & gt; NewStringUTF ("UTF-8"));
  int len ​​= env- & gt; GetArrayLength (stringJbytes);
  unsigned char * buf = new unsigned char [len];
  env- & gt; GetByteArrayRegion (stringJbytes, 0, len, reinterpret_cast & lt; jbyte * & gt; (buf));
  return buf;
}

gost34-13-15.h

# ifndef GOST341315_H
#define GOST341315_H
typedef unsigned char byte;
// ------------------------------------------------ --------------
/ *
Purpose:
Encrypt or decrypt incoming text.
Input parameters:
IV - sync. a package of length m (256 bits - 32 bytes)
text - input plain / cipher text of any length
size - text length
Return value: none
* /
void encrypt_decrypt_OFB (byte * IV, byte * text, int size);
// ------------------------------------------------ --------------
/ *
Purpose:
Clearing the sync array parcels.
Input parameters:
IV - sync. a package of length m (256 bits - 32 bytes)
Return value: none
* /
void closeGost34_13_15 (byte * IV);
// ------------------------------------------------ --------------
#endif // GOST341315_H

gost34-13-15.cpp

# include "gost34-12-15.h"
#include & lt; cstring & gt;
#include & lt; cstdio & gt;
#include & lt; string.h & gt;
using namespace std;
static int n = 16 ;;
// static int m = 32;
// static int s = 16;
byte text128 [16];
byte resultMSB_Y [16];
byte resultLSB [16];
static void MSB (byte * massive, int n);
static void LSB (byte * massive, int n);
void encrypt_decrypt_OFB (byte * R, byte * text, int size) {
  int q = size / n;
  int ostatok = size% n;
  for (int i = 0; i & lt; q; i ++) {
        arraycopy (text, i * n, text128, 0, n);
        MSB (R, n);
        encrypt128Gost34_12_15 (resultMSB_Y);
        X (text128, resultMSB_Y);
        LSB (R, n);
        arraycopy (resultLSB, 0, R, 0, n);
        arraycopy (resultMSB_Y, 0, R, n, n);
        arraycopy (text128, 0, text, i * n, n);
      }
  if (ostatok! = 0) {
    byte textLess128 [ostatok];
    byte resultMSBshort [ostatok];
    arraycopy (text, (q - 1) * n, textLess128, 0, ostatok);
    MSB (R, n);
    encrypt128Gost34_12_15 (resultMSB_Y);
    MSB (resultMSB_Y, ostatok);
    arraycopy (resultMSB_Y, 0, resultMSBshort, 0, ostatok);
    X (textLess128, resultMSBshort);
    arraycopy (textLess128, 0, text, (q - 1) * n, ostatok);
  }
}
static void MSB (byte * massive, int n) {
    arraycopy (massive, 0, resultMSB_Y, 0, n);
}
static void LSB (byte * massive, int n) {
    arraycopy (massive, n, resultLSB, 0, n);
}
void closeGost34_13_15 (byte * R) {
    memset (R, 0, 32);
}

gost34-12-15.h

# ifndef GOST341215_H
#define GOST341215_H
typedef unsigned char byte;
// ------------------------------------------------ --------------
/ *
Purpose:
Create an array of iteration keys
Input parameters:
current_key - 32 bytes long key to use
text - 16 byte text
length_text - text size
Return value: none
* /
void init128Gost34_12_15 (byte * current_key); //, int length_current_key, byte * text, int length_text);
// ------------------------------------------------ --------------
/ *
Purpose:
Copying data from array masIn from position pos1 to array masOut
from position pos2. The number of characters is length
Input parameters:
masIn - the array from where the data is copied; pos1 - in array masIn,
from which the data is copied; masOut - the array where to copy and
data is recorded; pos2 - the position in the masOut array from which
recording is in progress: length - the number of copied elements
* /
void arraycopy (byte * masIn, int pos1, byte * masOut, int pos2, int length);
// ------------------------------------------------ --------------
/ *
!!! Users, before using this function,
must call initK function once
Purpose:
Encrypts text
Input parameters:
msg - the text to be encrypted; msg_size - text size
Return value: none
* /
void encrypt128Gost34_12_15 (byte * msg);
// ------------------------------------------------ --------------
/ *
Purpose:
Execution of function X, it is also the addition of two arguments mod2
Input parameters:
res_arg1 - the first array that overwrites the result
operations; arg2 - the second array, which is added mod2 with
the first
* /
void X (byte * res_and_arg1, const byte * arg2);
// ------------------------------------------------ --------------
/ *
Purpose:
Copying data from array masIn from position pos1 to array masOut
from position pos2. The number of characters is length
Input parameters:
masIn - the array from where the data is copied; pos1 - in array masIn,
from which the data is copied; masOut - the array where to copy and
data is recorded; pos2 - the position in the masOut array from which
recording is in progress: length - the number of copied elements
* /
// void arraycopy (byte * masIn, int pos1, byte * masOut, int pos2, int length);
// ------------------------------------------------ --------------
void close128Gost34_12_15 ();
#endif // GOST341215_H

gost34-12-15.cpp

# include "gost34-12-15.h"
#include & lt; cstring & gt;
#include & lt; string.h & gt;
using namespace std;
const byte pi [] = {
  // 0 1 2 3 4 5 6 7
   252, 238, 221, 17, 207, 110, 49, 22, // 0
   251, 196, 250, 218, 35, 197, 4, 77, // 1
   233, 119, 240, 219, 147, 46, 153, 186, // 2
   23, 54, 241, 187, 20, 205, 95, 193, // 3
   249, 24, 101, 90, 226, 92, 239, 33, // 4
   129, 28, 60, 66, 139, 1, 142, 79, // 5
   5, 132, 2, 174, 227, 106, 143, 160, // 6
   6, 11, 237, 152, 127, 212, 211, 31, // 7
   235, 52, 44, 81, 234, 200, 72, 171, // 8
   242, 42, 104, 162, 253, 58, 206, 204, // 9
   181, 112, 14, 86, 8, 12, 118, 18, // 10
   191, 114, 19, 71, 156, 183, 93, 135, // 11
   21, 161, 150, 41, 16, 123, 154, 199, // 12
   243, 145, 120, 111, 157, 158, 178, 177, // 13
   50, 117, 25, 61, 255, 53, 138, 126, // 14
   109, 84, 198, 128, 195, 189, 13, 87, // 15
   223, 245, 36, 169, 62, 168, 67, 201, // 16
   215, 121, 214, 246, 124, 34, 185, 3, // 17
   224, 15, 236, 222, 122, 148, 176, 188, // 18
   220, 232, 40, 80, 78, 51, 10, 74, // 19
   167, 151, 96, 115, 30, 0, 98, 68, // 20
   26, 184, 56, 130, 100, 159, 38, 65, // 21
   173, 69, 70, 146, 39, 94, 85, 47, // 22
   140, 163, 165, 125, 105, 213, 149, 59, // 23
7, 88, 179, 64, 134, 172, 29, 247, // 24
   48, 55, 107, 228, 136, 217, 231, 137, // 25
   225, 27, 131, 73, 76, 63, 248, 254, // 26
   141, 83, 170, 144, 202, 216, 133, 97, // 27
   32, 113, 103, 164, 45, 43, 9, 91, // 28
   203, 155, 37, 208, 190, 229, 108, 82, // 29
   89, 166, 116, 210, 230, 244, 180, 192, // 30
   209, 102, 175, 194, 57, 75, 99, 182}; // 31
const byte pi_reverse [] = {
   165, 45, 50, 143, 14, 48, 56, 192,
   84, 230, 158, 57, 85, 126, 82, 145,
   100, 3, 87, 90, 28, 96, 7, 24,
   33, 114, 168, 209, 41, 198, 164, 63,
   224, 39, 141, 12, 130, 234, 174, 180,
   154, 99, 73, 229, 66, 228, 21, 183,
   200, 6, 112, 157, 65, 117, 25, 201,
   170, 252, 77, 191, 42, 115, 132, 213,
   195, 175, 43, 134, 167, 177, 178, 91,
   70, 211, 159, 253, 212, 15, 156, 47,
   155, 67, 239, 217, 121, 182, 83, 127,
   193, 240, 35, 231, 37, 94, 181, 30,
   162, 223, 166, 254, 172, 34, 249, 226,
   74, 188, 53, 202, 238, 120, 5, 107,
   81, 225, 89, 163, 242, 113, 86, 17,
   106, 137, 148, 101, 140, 187, 119, 60,
   123, 40, 171, 210, 49, 222, 196, 95,
   204, 207, 118, 44, 184, 216, 46, 54,
   219, 105, 179, 20, 149, 190, 98, 161,
   59, 22, 102, 233, 92, 108, 109, 173,
   55, 97, 75, 185, 227, 186, 241, 160,
   133, 131, 218, 71, 197, 176, 51, 250,
   150, 111, 110, 194, 246, 80, 255, 93,
   169, 142, 23, 27, 151, 125, 236, 88,
   247, 31, 251, 124, 9, 13, 122, 103,
   69, 135, 220, 232, 79, 29, 78, 4,
   235, 248, 243, 62, 61, 189, 138, 136,
   221, 205, 11, 19, 152, 2, 147, 128,
   144, 208, 36, 52, 203, 237, 244, 206,
   153, 16, 68, 64, 146, 58, 1, 38,
   18, 26, 72, 104, 245, 129, 139, 199,
214, 32, 10, 8, 0, 76, 215, 116};
void initK (byte * inKey); // the key is 32 bytes
static void LSX (byte * key, const byte * arg); // process of functions L, S and X: 16 bytes of the key are supplied
static void L (byte *); // L transformation, receives an array of 16 bytes as input: in one case, the number
                        // element i as an array, otherwise an array after R transformation
static void F (byte * masC, byte * x, byte * y); // F transformation, at the input an array of iterative constants masC 16 bytes,
                        // arrays of 16 bytes x and y, which are the expansion of the 32 byte key
                        // on K1 and K2, 16 bytes each
static void S (byte * masLSX_XSL); // transform S, which receives an array of 16 bytes after X
static byte mul_gf256 (byte x, byte y); // mul function
static void R (byte * arg); // transform R, an array of 16 bytes is received at the input
static byte l (byte * arg); // transform l
// array of l substitutions
const byte lNumbers [] = {
  148, 32, 133, 16, 194, 192, 1, 251,
  1, 192, 194, 16, 133, 32, 148, 1};
static byte iterK [10] [16];
static byte masC [32] [16];
static byte masLSX_XSL [16];
static bool isMasC = false;
static void GET_MAS_C () {
  for (int i = 0; i & lt; 32; i ++) {
    memset (masC [i], 0, 15);
    masC [i] [15] = (byte) (i + 1);
    L (masC [i]);
  }
}
void init128Gost34_12_15 (byte * key) {
  if (! isMasC) {
    GET_MAS_C ();
    isMasC = true;
  }
  initK (key);
}
void initK (byte * inKey) {
  byte x [16];
  byte y [16];
  for (int i = 0; i & lt; 16; i ++) {
    iterK [0] [i] = x [i] = inKey [i];
    iterK [1] [i] = y [i] = inKey [i + 16];
  }
for (int i = 1; i & lt; 5; i ++) {
    int q = 0;
    for (int j = 1; j & lt; 9; j ++) {
      F (masC [(8 * (i - 1) + j) - 1], x, y);
    }
    arraycopy (x, 0, iterK [2 * i], 0, 16);
    arraycopy (y, 0, iterK [2 * i + 1], 0, 16);
  }
}
void close128Gost34_12_15 () {
  for (int i = 0; i & lt; 32; i ++)
    memset (masC [i], 0, 16);
  for (int i = 0; i & lt; 10; i ++)
    memset (iterK [i], 0, 16);
}
void encrypt128Gost34_12_15 (byte * text) {
   byte block [16];
   arraycopy (text, 0, block, 0, 16);
   for (int i = 0; i & lt; 9; i ++) {// 16 bytes long
     LSX (iterK [i], block);
     memmove (block, masLSX_XSL, 16);
   }
   X (block, iterK [9]);
   arraycopy (block, 0, text, 0, 16);
}
static void F (byte * key, byte * arg1, byte * arg0) {
  LSX (key, arg1);
  X (masLSX_XSL, arg0);
  arraycopy (arg1, 0, arg0, 0, 16);
  arraycopy (masLSX_XSL, 0, arg1, 0, 16);
}
static void LSX (byte * key, const byte * arg) {
  memmove (masLSX_XSL, key, 16);
  X (masLSX_XSL, arg);
  S (masLSX_XSL);
  L (masLSX_XSL);
}
static void S (byte * arg) {
  for (int i = 15; i & gt; = 0; i--)
    arg [i] = pi [arg [i]];
}
static byte l (byte * arg) {
  byte sum = arg [15];
  for (int i = 14; i & gt; = 0; i--)
    sum ^ = mul_gf256 (arg [i], lNumbers [i]);
  return sum;
}
static void R (byte * arg) {
  byte z = l (arg);
  byte buffer [16] = {0};
  arraycopy (arg, 0, buffer, 1, 15);
  arraycopy (buffer, 1, arg, 1, 15);
  arg [0] = z;
}
static void L (byte * arg) {
  for (int i = 0; i & lt; 16; i ++)
    R (arg);
}
void X (byte * arg1, const byte * arg2) {
  for (int i = 0; i & lt; 16; i ++)
    arg1 [i] ^ = arg2 [i];
}
static byte mul_gf256 (byte x, byte y)
{
  byte z = 0x0;
  while (y)
  {
    if (y & amp; 1)
      z ^ = x;
x = (x & lt; & lt; 1) ^ (x & amp; 0x80? 0xC3: 0x00);
    y & gt; & gt; = 1;
  }
  return z;
}
void arraycopy (byte * masIn, int pos1, byte * masOut, int pos2, int length) {
  for (int i = pos1; i & lt; (length + pos1); i ++) {
    // masIn [i] = masOut [pos2];
    masOut [pos2] = masIn [i];
    pos2 ++;
  }
}

Answer 1

Usually, an unknown link is a symptom of the following problem:

  • Not included header with symbol declaration;
  • The file with the symbol implementation is not connected (c , lib or dll );
  • If the symbol implementation is in binary (lib or dll ), this binary might be incompatible with the current compiler, for example, due to mismatched bitness.

Answer 2

I seem to have decided, but I forgot to write right away.
In general, as I understand it, the problem was that I did not register the .cpp and .h files used in CMakeList.

add_library (# Sets the name of the library.
    native-lib
    # Sets the library as a shared library.
    SHARED
    # Provides a relative path to your source file (s).
    src / main / cpp / native-lib.cpp
    src / main / cpp / gost34-12-15.cpp
    src / main / cpp / gost34-12-15.h
    src / main / cpp / gost34-13-15.cpp
    src / main / cpp / gost34-13-15.h)

Here. True, now another problem has arisen. There are no errors, the program starts. But for some reason it is not encrypted.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions