Linking issues using OpenSSL in Ubuntu - c++

I have installed OpenSSL using sudo apt-get install openssl-dev. When I try to compile it using Netbeans it gives following errors. How can I fix this problem?
g++ -lssl -o dist/Debug/GNU-Linux-x86/cppapplication_2 build/Debug/GNU-Linux-x86/main.o -L/home/sercan/Desktop/openssl-0.9.8h-1-lib/lib
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:46: undefined reference to `OPENSSL_add_all_algorithms_noconf'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:48: undefined reference to `ERR_load_crypto_strings'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:58: undefined reference to `d2i_PKCS12_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:66: undefined reference to `ERR_print_errors_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:72: undefined reference to `PKCS12_parse'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:76: undefined reference to `ERR_print_errors_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:82: undefined reference to `PKCS12_free'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:96: undefined reference to `PEM_write_PrivateKey'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:104: undefined reference to `PEM_write_X509_AUX'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:108: undefined reference to `sk_num'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `sk_value'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `PEM_write_X509_AUX'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:112: undefined reference to `sk_num'
My code is here:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>
/* Simple PKCS#12 file reader */
int main(int argc, char **argv)
{
FILE *fp;
EVP_PKEY *pkey;
X509 *cert;
STACK_OF(X509) *ca = NULL;
PKCS12 *p12;
int i;
if (argc != 4) {
fprintf(stderr, "Usage: pkread p12file password opfile\n");
exit(1);
}
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
if (!(fp = fopen(argv[1], "rb"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp);
if (!p12) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit(1);
}
if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit(1);
}
PKCS12_free(p12);
if (!(fp = fopen(argv[3], "w"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
if (pkey) {
fprintf(fp, "***Private Key***\n");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
}
if (cert) {
fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUX(fp, cert);
}
if (ca && sk_X509_num(ca)) {
fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
}
fclose(fp);
return 0;
}

you need to link to libcrypto as well - add -lcrypto to the link line and your code should link correctly.
natsu:~/openssl% gcc -o test test.c -L/usr/lib -lssl -lcrypto
links correctly, while:
natsu:~/openssl% gcc -o test test.c -L/usr/lib -lssl
/tmp/ccvA7iNe.o: In function `main':
test.c:(.text+0x4c): undefined reference to `OPENSSL_add_all_algorithms_noconf'
test.c:(.text+0x51): undefined reference to `ERR_load_crypto_strings'
test.c:(.text+0xb9): undefined reference to `d2i_PKCS12_fp'
test.c:(.text+0x103): undefined reference to `ERR_print_errors_fp'
test.c:(.text+0x133): undefined reference to `PKCS12_parse'
test.c:(.text+0x16a): undefined reference to `ERR_print_errors_fp'
test.c:(.text+0x180): undefined reference to `PKCS12_free'
test.c:(.text+0x22c): undefined reference to `PEM_write_PrivateKey'
test.c:(.text+0x266): undefined reference to `PEM_write_X509_AUX'
test.c:(.text+0x27b): undefined reference to `sk_num'
test.c:(.text+0x2b7): undefined reference to `sk_value'
test.c:(.text+0x2c9): undefined reference to `PEM_write_X509_AUX'
test.c:(.text+0x2d9): undefined reference to `sk_num'
collect2: ld returned 1 exit status
does not.

Related

Error getting output from mysql.h using C++ and g++

I have a small piece of code which (should) allows me to connect to an MySQL database, here is the code:
#include <iostream>
#include <mariadb/mysql.h> // /usr/includes/mariadb/mysql.h
struct connection_details
{
const char *server, *user, *password, *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details){
MYSQL *connection = mysql_init(NULL); // mysql instance
//connect database
if(!mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)){
std::cout << "Connection Error: " << mysql_error(connection) << std::endl;
exit(1);
}
return connection;
}
// mysql_res = mysql result
MYSQL_RES* mysql_perform_query(MYSQL *connection, const char *sql_query){
//send query to db
if(mysql_query(connection, sql_query)){
std::cout << "MySQL Query Error: " << mysql_error(connection) << std::endl;
exit(1);
}
return mysql_use_result(connection);
}
int main(int argc, char const *argv[])
{
MYSQL *con; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "netser"; // the root user of mysql
mysqlD.password = "root"; // the password of the root user in mysql
mysqlD.database = "mydatabase"; // the databse to pick
// connect to the mysql database
con = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(con, "show tables");
std::cout << ("MySQL Tables in mysql database:") << std::endl;
while ((row = mysql_fetch_row(res)) !=NULL)
std::cout << row[0] << std::endl;
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(con);
return 0;
}
When I try to compile it using:
g++ connectdb.cpp -o output && ./output
I get the next few errors:
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `mysql_connection_setup(connection_details)':
connectdb.cpp:(.text+0xf): undefined reference to `mysql_init'
/usr/bin/ld: connectdb.cpp:(.text+0x3c): undefined reference to `mysql_real_connect'
/usr/bin/ld: connectdb.cpp:(.text+0x6c): undefined reference to `mysql_error'
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `mysql_perform_query(st_mysql*, char const*)':
connectdb.cpp:(.text+0xc4): undefined reference to `mysql_query'
/usr/bin/ld: connectdb.cpp:(.text+0xef): undefined reference to `mysql_error'
/usr/bin/ld: connectdb.cpp:(.text+0x125): undefined reference to `mysql_use_result'
/usr/bin/ld: /tmp/ccDwpmw3.o: in function `main':
connectdb.cpp:(.text+0x1ca): undefined reference to `mysql_fetch_row'
/usr/bin/ld: connectdb.cpp:(.text+0x213): undefined reference to `mysql_free_result'
/usr/bin/ld: connectdb.cpp:(.text+0x21f): undefined reference to `mysql_close'
{By the way, I use Parrot OS with Mariadb and use VSCode}
I have never worked with databases in C++ before, so I have barely any idea what could be the problem, but I can assure you that the database does exit and that the connection to mysql.h has no problems...
The problem was how I was executing it, I had to use
g++ connectdb.cpp -o output -L/usr/include/mariadb/mysql -lmariadbclient
And not
g++ connectdb.cpp -o output
For future people with the same problem, here's a breakdown:
g++ connectdb.cpp -o output (normal compile)
-L/usr/include/mariadb/mysql (depending on what OS you have and if you use mariadb, you may have to change this to something like -L/usr/include/mysql instead, if you do use mariadb and still have a problem, remember to apt-get install libmariadb-dev)
-lmariadbclient (You have to apt-get install mariadb... If you're using MySQL, use -lmysqlclient instead)

Use opencv C++ on raspberry pi 2

I am successfully running opencv python code on raspberry pi 2 (raspbian).
Now I want to try to compile opencv C++ code on raspberry pi 2 by using this command:
g++ -std=c++0x test_colour_tracking_1.cpp -otest_colour
The C++ coding as below.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("image",imgOriginal);
}
return 0;
}
But it show error as below.
/tmp/ccHcCqSm.o: In function `main':
test_colour_tracking_1.cpp:(.text+0x70): undefined reference to `cv::VideoCapture::VideoCapture(int)'
test_colour_tracking_1.cpp:(.text+0x7c): undefined reference to `cv::VideoCapture::isOpened() const'
test_colour_tracking_1.cpp:(.text+0xd8): undefined reference to `cv::VideoCapture::read(cv::Mat&)'
test_colour_tracking_1.cpp:(.text+0x150): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_colour_tracking_1.cpp:(.text+0x164): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_colour_tracking_1.cpp:(.text+0x1a4): undefined reference to `cv::VideoCapture::~VideoCapture()'
test_colour_tracking_1.cpp:(.text+0x1f0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/ccHcCqSm.o: In function `cv::Mat::~Mat()':
test_colour_tracking_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccHcCqSm.o: In function `cv::Mat::release()':
test_colour_tracking_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x58): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
And I want to ask how to check frame rate per second?
Try to use the following command:
g++ -std=c++0x test_colour_tracking_1.cpp -o test_colour `pkg-config --cflags --libs opencv`

OpenGL project returns with undefined references

I am following a tutorial for OpenGL from this site. I have downloaded and installed (hopefully correctly) the OpenGL libraries that are used. (GLEW, GLFW, GLM). However, when I compile the code from the site, I find a lot of errors with undefined references.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialize GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetWindowTitle( "Playground" );
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Dark blue background
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
Errors:
"make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
"make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/opengl_1.exe
make[2]: Entering directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
mkdir -p dist/Debug/MinGW-Windows
g++.exe -o dist/Debug/MinGW-Windows/opengl_1 build/Debug/MinGW-Windows/main.o
build/Debug/MinGW-Windows/main.o: In function `main':
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:21: undefined reference to `glfwInit'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:27: undefined reference to `glfwOpenWindowHint'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:28: undefined reference to `glfwOpenWindowHint'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:29: undefined reference to `glfwOpenWindowHint'
nbproject/Makefile-Debug.mk:62: recipe for target `dist/Debug/MinGW-Windows/opengl_1.exe' failed
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:30: undefined reference to `glfwOpenWindowHint'
make[2]: Leaving directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:31: undefined reference to `glfwOpenWindowHint'
nbproject/Makefile-Debug.mk:59: recipe for target `.build-conf' failed
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:34: undefined reference to `glfwOpenWindow'
make[1]: Leaving directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:37: undefined reference to `glfwTerminate'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:42: undefined reference to `_imp__glewInit#0'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:47: undefined reference to `glfwSetWindowTitle'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:50: undefined reference to `glfwEnable'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:53: undefined reference to `glClearColor#16'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:59: undefined reference to `glfwSwapBuffers'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:62: undefined reference to `glfwGetKey'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:63: undefined reference to `glfwGetWindowParam'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:66: undefined reference to `glfwTerminate'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/opengl_1.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 875ms)
Note: I'm using Netbeans (with C++ plugin) as an IDE.
Re-reading the makefile output, I see you don't actually link with the GL libraries. You need to modify the project setting to include the libraries.

Installing OpenCV2.4.1 on windows 7 x64 - Mingw & codeblocks

Installing OpenCV2.4.1 on windows 7 x64 - Mingw & codeblocks
Hi,
I followed this tutorial
Getting started with OpenCV 2.4 and MinGW on Windows 7
I configured the same except instead of x86, i put x64.
This is the example
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
when i compile main.cpp
C:\Users\rgap\Desktop>g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x64\mingw\lib" main.cpp -lope
ncv_core241 -lopencv_highgui241 -o main
I get the following error
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x62): undefined reference to `cv::imread(
std::string const&, int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xc7): undefined reference to `cv::_InputA
rray::_InputArray(cv::Mat const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xfe): undefined reference to `cv::imshow(
std::string const&, cv::_InputArray const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x120): undefined reference to `cv::waitKe
y(int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x2b): und
efined reference to `cv::fastFree(void*)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[cv::Mat::release()]+0
x3c): undefined reference to `cv::Mat::deallocate()'
collect2: ld devolvió el estado de salida 1
Is this a bug? :(
thanks :)

Using libXml2 in C++ with Netbeans

I'm having a problem getting C++ code to compile properly in Netbeans, specifically code that deals with libXml2. I downloaded libXml2, put it in the include folder and I know that the code itself compiles fine without any libxml2 references in it, however, when I add the following functions:
void XmlParser::processNode(xmlTextReaderPtr reader){
const xmlChar *name, *value;
name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";
value = xmlTextReaderConstValue(reader);
printf("%d %d %s %d %d",
xmlTextReaderDepth(reader),
xmlTextReaderNodeType(reader),
name,
xmlTextReaderIsEmptyElement(reader),
xmlTextReaderHasValue(reader));
if (value == NULL)
printf("\n");
else {
if (xmlStrlen(value) > 40)
printf(" %.40s...\n", value);
else
printf(" %s\n", value);
}
}
void XmlParser::streamFile(const char *filename) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
//processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
}
I get the following result when I click build:
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser11processNodeEP14_xmlTextReader':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:26: undefined reference toxmlTextReaderConstName'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:30: undefined reference to xmlTextReaderConstValue'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference toxmlTextReaderHasValue'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderIsEmptyElement'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference toxmlTextReaderNodeType'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderDepth'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:41: undefined reference toxmlStrlen'
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser10streamFileEPKc':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:51: undefined reference toxmlReaderForFile'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:53: undefined reference to xmlTextReaderRead'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:56: undefined reference toxmlTextReaderRead'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:58: undefined reference to `xmlFreeTextReader'
Which suggests that I have some sort of an issue compiling the program using libXml2. I saw that someone right here had the same error, and that the answer to this problem is correctly setting up the argument for compiling the program, however, I cannot figure out how to do this in NetBeans/Windows. I think what I need is details on how to actually get the arguments right. Any help would be appreciated. Thank you.
Add -lxml2 (gcc) or libxml2.lib (visual studio) to your linker.