Hash function SHA1 in C++ [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I want to use SHA1 function from openssl library for hashing a string, I have downloaded the library and installed it in /usr/include, and here is my code:
#include <openssl/sha.h>
#include <string.h>
#include <stdio.h>
int main() {
unsigned char digest[SHA_DIGEST_LENGTH];
char string[] = "hello world";
SHA1((unsigned char*) &string, strlen(string), (unsigned char*) &digest);
}
It doesn't have any syntax error and it recognizes openssl/sha.h, but when I want to build the project in eclipse or build from the terminal, I get this error:
Hash.cpp:(.text+0x4a): undefined reference to `SHA1'
collect2: error: ld returned 1 exit status
Any help would be appreciated! :)

You didn't properly link openssl, if you're on Linux, you should link crypto.
From a terminal :
g++ -o hash hash.cpp -lcrypto
From eclipse, you should open project->Properties, go to C/C++ Build->Settings and add crypto in the Linker->Libraries folder.

Related

Visual Studio Code can't find reference to function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
I'm trying to use Visual Studio for a specific project but I can't get files to link properly.
When including a header file with a defined function in another cpp file im getting an error undefined reference to testFunc() collect2.exe: error: ld returned 1 exit status
Thing is, this exact same code works perfectly in Eclipse. Can someone tell me what I'm doing wrong?
Test.cpp
#include "Other.h"
int main(){
testFunc();
return 0;
}
Other.h
#pragma once
void testFunc();
Other.cpp
#include "Other.h"
#include <iostream>
using namespace std;
void testFunc(){
cout << "HelloWorld";
}
When Buildung, this occours:
Starting build...
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
C:\Users\johan\AppData\Local\Temp\cck3aAZo.o: In function `main':
C:/Users/johan/cu-workspace/TEst/Test.cpp:5: undefined reference to `testFunc()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
If you build Other.h and Other.cpp as a project, then you need to configure the linker to add Other.lib into test project.
For a simple scenario, you can have all 3 files in one project and they should build just fine.
According to your build info:
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
You can see that Other.cpp is not in your project, so you might need to add it into your project.
Since you are using VS code, you can write a simple command in terminal to build your code:
C:\MinGW\bin\g++.exe -g C:\Users\johan\cu-workspace\TEst\Test.cpp C:\Users\johan\cu-workspace\TEst\Other.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe

Functions of Open AL giving error of undefined reference while compiling with g++ in ubuntu [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am completely new to Open AL.
So I started with installing Open AL library through command line
sudo apt-get install libopenal-dev
And also I installed alut installed with this command
sudo apt-get install libalut0 libalut-dev
Also I forked open Al from http://kcat.strangesoft.net/openal.html
and installed it also.
But when I am trying to compile this simple program:
#include <stdio.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>
int main(){
ALCdevice *device;
device=alcOpenDevice(NULL);
if(!device)
{
printf("no device found");
}
else
{
printf("device found");
}
return 0;
}
I am getting this error:
/tmp/cchMpaeS.o: In function main':
altest.cpp:(.text+0xe): undefined reference toalcOpenDevice'
collect2: error: ld returned 1 exit status
I compiled it with
g++ altest.cpp
g++ -std=c++11 altest.cpp
Both of them gave same error.
You need to link to the OpenAl library:
g++ -std=c++11 altest.cpp -lopenal

Compile GLFW code with g++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am just starting to get into C++ coding, specifically OpenGL (GLFW at this stage). I am using MinGW, and compiling my code using a batch file:
g++ -time -v -I lib/glfw-3.1.2/include -o "bin/OpenGL Test" src/Main.cpp
My test code is this:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if(!glfwInit()) {
return 1;
}
glfwTerminate();
return 0;
}
To me this seems like it should compile and run correctly, but it throws errors at the two GLFW functions, claiming that they are 'undefined references'.
What have I done wrong?
'undefined references' is a common error. You should compile your cpp first. Then link it with the library to form executable file.

Cannot compile with boost? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
Attempting to create a simple WebSocket c++ application, the library I am using relies on boost.
I have attempted to add boost to my project but it refuses to compile, heres my cpp file
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
main()
{
printf("Hello World\n");
return 0;
}
I am attemting to compile it with g++ -I. -Iboost_1_58_0 /home/cabox/workspace/main.cpp -o /home/cabox/workspace/bin/exe, but all i get is
In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0xfba): undefined reference to`boost::system::generic_category()'
My folder layout is just my Main.cpp file, the boost_1_58_0 & the websocketpp folders all being in the same parent directory, what is it I'm doing wrong?
Add
-lboost_system
(see also What is an undefined reference/unresolved external symbol error and how do I fix it?)
Optionally specify the library directory with -L
Make sure it's after the cpps
Undefined reference to boost::system::generic_category despite linking with boost_system

C++ Hello world won't compile? Seems to be a linking issue… [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
This very simple C++ program using the standard library doesn't compile with GCC
(5 answers)
Closed 8 years ago.
The following Hello World program compiles inside Xcode, but not when compiled with via clang in the Terminal:
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
specifically, the command i'm using is:
clang c++test.cpp
where c++test is the name of the file.
This produces a bunch of gibberish errors like:
(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*,
char const*, std::__1::ios_base&, char) in c++test-497cf6.o
As well as this:
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked and the c++ libs appear present on the system, so I'm pretty sure I'm doing something work. Is there a -framework I need to link to?
If you compile/link C++, use (clan)g++. This will ensure the C++ standard library is also linked in.
Alternatively, add -lstdc++ or in your case -lc++ to the link command. I would just call clang++ though.