title: How to solve undefined reference to `AES_ctr128_encrypt'
body: <p>Hello I was studying aes 128 encryption with this link <a href="https://www.gurutechnologies.net/blog/aes-ctr-encryption-in-c/" rel="nofollow noreferrer">https://www.gurutechnologies.net/blog/aes-ctr-encryption-in-c/</a>.
  But to run the example main.c
  Compiled as follows</p>
  
  <pre><code>gcc main.c -lm -lcrypto -lssl -o mai.c
  </code></pre>
  
  <p>Then I get the following error:</p>
  
  <pre><code>main.c: In function ‘fencrypt’:
  main.c:83:3: warning: implicit declaration of function ‘AES_ctr128_encrypt’; did you mean ‘AES_cfb128_encrypt’? [-Wimplicit-function-declaration]
     AES_ctr128_encrypt(indata, outdata, bytes_read, &amp;key, state.ivec, state.ecount, &amp;state.num);
     ^~~~~~~~~~~~~~~~~~
     AES_cfb128_encrypt
  /tmp/cc1Gq6eV.o: In function `fencrypt':
  main.c:(.text+0x253): undefined reference to `AES_ctr128_encrypt'
  /tmp/cc1Gq6eV.o: In function `fdecrypt':
  main.c:(.text+0x449): undefined reference to `AES_ctr128_encrypt'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>To fix this I go into the directory where openssl is located, and in the aes.h header file
  I tried to find the AES_ctr128_encrypt function, but it did not
  So I updated the openssl and confirmed that there is a AES_ctr128_encrypt function in aes.h and tried to recompile it, but nothing changed. I will leave a question because I could not find a solution anymore. </p>
  
  <p>This is the code for AES_ctr128_encrypt</p>
  
  <pre><code>void fencrypt(char* read, char* write, const unsigned char* enc_key)
  { 
      if(!RAND_bytes(iv, AES_BLOCK_SIZE))
      {
          fprintf(stderr, "Could not create random bytes.");
          exit(1);    
      }
  
      readFile = fopen(read,"rb"); // The b is required in windows.
      writeFile = fopen(write,"wb");
  
      if(readFile==NULL)  
      {
          fprintf(stderr, "Read file is null."); 
          exit(1);
      }
  
      if(writeFile==NULL)
      {
          fprintf(stderr, "Write file is null."); 
          exit(1);
      }
  
      fwrite(iv, 1, 8, writeFile); // IV bytes 1 - 8
      fwrite("\0\0\0\0\0\0\0\0", 1, 8, writeFile); // Fill the last 4 with null bytes 9 - 16
  
      //Initializing the encryption KEY
      if (AES_set_encrypt_key(enc_key, 128, &amp;key) &lt; 0)
      {
          fprintf(stderr, "Could not set encryption key.");
          exit(1); 
      }
  
      init_ctr(&amp;state, iv); //Counter call
      //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext  
      while(1)    
      {
          bytes_read = fread(indata, 1, AES_BLOCK_SIZE, readFile); 
          AES_ctr128_encrypt(indata, outdata, bytes_read, &amp;key, state.ivec, state.ecount, &amp;state.num);
  
          bytes_written = fwrite(outdata, 1, bytes_read, writeFile); 
          if (bytes_read &lt; AES_BLOCK_SIZE)
          {
              break;
          }
      }
  
      fclose(writeFile);
      fclose(readFile);
  }
  
  void fdecrypt(char* read, char* write, const unsigned char* enc_key)
  {   
  
      readFile=fopen(read,"rb"); // The b is required in windows.
      writeFile=fopen(write,"wb");
  
      if(readFile==NULL)
      {
          fprintf(stderr,"Read file is null."); 
          exit(1);
      }
  
      if(writeFile==NULL) 
      {
          fprintf(stderr, "Write file is null."); 
          exit(1);
      }
  
      fread(iv, 1, AES_BLOCK_SIZE, readFile); 
  
      //Initializing the encryption KEY
      if (AES_set_encrypt_key(enc_key, 128, &amp;key) &lt; 0)
      {
          fprintf(stderr, "Could not set decryption key.");
          exit(1);
      }
  
      init_ctr(&amp;state, iv);//Counter call
      //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext       
      while(1)    
      {
          bytes_read = fread(indata, 1, AES_BLOCK_SIZE, readFile);    
          //printf("%i\n", state.num);
          AES_ctr128_encrypt(indata, outdata, bytes_read, &amp;key, state.ivec, state.ecount, &amp;state.num);
  
          bytes_written = fwrite(outdata, 1, bytes_read, writeFile); 
          if (bytes_read &lt; AES_BLOCK_SIZE) 
          {
              break;
          }
      }
      fclose(writeFile); 
      fclose(readFile); 
  }
  </code></pre>
  
  <p>Thank you in advance.</p>