OpenGL project returns with undefined references - c++

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.

Related

Use SDL2 with CLion 2020

I can't seem to link SDL2 correctly. I am using CLion with MinGW. My CMakeLists.txt looks like this currently, but I tried using the FindSDL2 module floating around, though it always said that it could not find the SDL2 library. This is the only way I've found it to stop giving back errors.
cmake_minimum_required(VERSION 3.16)
project(CBeat)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "CC:/Development/SDL2_MinGW_32bit/lib/cmake/SDL2")
set(SDL2_DIR C:/Mingw)
set(CMAKE_CXX_STANDARD 14)
set(SDL2_LIB_DIR ${SDL2_DIR}/lib)
include_directories(${SDL2_DIR}/include)
add_executable(CBeat main.cpp)
target_link_libraries(CBeat ${SDL2_LIB_DIR}/libSDL2.dll.a ${SDL2_LIB_DIR}/libSDL2main.a ${SDL2_LIB_DIR}/libSDL2.a -mwindows -lmingw32 -lSDL2main -lSDL2)
add_definitions(-DSDL_MAIN_HANDLED)
Now, the main.cpp looks like this, CLion shows no errors and actually is able to give me suggestions and information in each function, but when running it marks various functions as undefined. I attach evidence. I am so desperate right now, I've been trying to figure out this for days now and nothing seems to work!
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
const int SWIDTH=640, SHEIGHT=420;
int main( int argc, char* args[] ){
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
cout<< "SDL could not initialize! SDL_Error:" << SDL_GetError << endl;
}
else
{
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SWIDTH, SHEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
cout << "Window could not be created! SDL_Error: " << SDL_GetError << endl;
}
else
{
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL,SDL_MapRGB(screenSurface->format, 0xFF,0xFF,0xFF));
SDL_UpdateWindowSurface( window );
SDL_Delay( 2000 );
}
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
}
CLion trying to be useful
And these are the errors it gives back:
[ 50%] Linking CXX executable CBeat.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\CBeat.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/yello/CLionProjects/CBeat/main.cpp:12: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:18: undefined reference to `SDL_CreateWindow'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:25: undefined reference to `SDL_GetWindowSurface'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:26: undefined reference to `SDL_MapRGB'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:26: undefined reference to `SDL_FillRect'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:27: undefined reference to `SDL_UpdateWindowSurface'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:28: undefined reference to `SDL_Delay'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:30: undefined reference to `SDL_DestroyWindow'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/yello/CLionProjects/CBeat/main.cpp:31: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\CBeat.dir\build.make:88: recipe for target 'CBeat.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/CBeat.dir/all' failed
mingw32-make.exe[3]: *** [CBeat.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/CBeat.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/CBeat.dir/rule] Error 2
mingw32-make.exe: *** [CBeat] Error 2
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/CBeat.dir/rule' failed
Makefile:117: recipe for target 'CBeat' failed

How do I link a static GLEW library to a CMake project using MinGW?

I'm trying to make a simple triangle project to learn OpenGL, but I'm stuck at a compile error:
GLFWwindow* initApp() {
if (!glfwInit()) {
std::cerr << "Failed to create GLFW context" << std::endl;
return nullptr;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(1280, 720, "TriangleApp", nullptr, nullptr);
if (window == nullptr) {
std::cerr << "Failed to open GLFW window" << std::endl;
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (!glewInit()) {
std::cerr << "Failed to initialize GL libraries" << std::endl;
return nullptr;
}
return window;
}
CMake is telling me there's an undefined reference to glewInit and glewExperimental. I have included glew.h before any OpenGL header, defined GLEW_STATIC in my CMake project and linked glfw3.a and glew32s.lib to the target using CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(TriangleApp)
# GLEW
add_compile_definitions(GLEW_STATIC)
include_directories(libraries/glew/include)
set(GLEW_LINK_LIBRARY ${CMAKE_SOURCE_DIR}/libraries/glew/lib/Release/Win32/glew32s.lib)
# GLFW
include_directories(libraries/glfw/include)
set(GLFW_LINK_LIBRARY ${CMAKE_SOURCE_DIR}/libraries/glfw/lib/libglfw3.a)
# My App
include_directories(headers)
add_executable(TriangleApp source/Main.cpp)
target_link_libraries(TriangleApp
${GLEW_LINK_LIBRARY}
${GLFW_LINK_LIBRARY})
Here's the output from the CMake Tasks extension (vscode):
[main] Building folder: TriangleApp
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/benjo/source/repos/windows/C++/OpenGL/TriangleApp/build --config Debug --target all -- -j 10
[build] Scanning dependencies of target TriangleApp
[build] [ 50%] Building CXX object CMakeFiles/TriangleApp.dir/source/Main.cpp.obj
[build] [100%] Linking CXX executable TriangleApp.exe
[build] CMakeFiles\TriangleApp.dir/objects.a(Main.cpp.obj): In function `initApp()':
[build] C:/Users/benjo/source/repos/windows/C++/OpenGL/TriangleApp/source/Main.cpp:27: undefined reference to `glewInit'
[build] CMakeFiles\TriangleApp.dir/objects.a(Main.cpp.obj):Main.cpp:(.rdata$.refptr.glewExperimental[.refptr.glewExperimental]+0x0): undefined reference to `glewExperimental'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\TriangleApp.dir\build.make:108: TriangleApp.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/TriangleApp.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:103: all] Error 2
[build] Build finished with exit code 2

mingw32-make undefined reference to `cv::imread(cv::String const&, int)' OPENCV

So I have OpenCV libraries downloaded from here
https://sourceforge.net/projects/opencvlibrary/files/3.4.6/opencv-3.4.6-vc14_vc15.exe/download
CMake gui with cmakelist.txt like this
cmake_minimum_required(VERSION 2.8)
project(Display)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library
status:") message(STATUS " config:
${OpenCV_DIR}") message(STATUS "
version: ${OpenCV_VERSION}")
message(STATUS " libraries:
${OpenCV_LIBS}") message(STATUS "
include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(Display
DisplayImage.cpp)
target_link_libraries(Display
${OpenCV_LIBS})
and have a simple cpp like this
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv )
{
// cout << "You have entered " << argc
// << " arguments:" << "\n";
// for (int i = 0; i < argc; ++i)
// cout << argv[i] << "\n";
// cout << "a" << argv[1];
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
printf("Hello World!");
return 0;
}
already configure it with cmake-gui and this shows up
OpenCV library status:
config: D:/opencv/build/x64/vc15/lib
version: 3.4.6
libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_world
include path: D:/opencv/build/include;D:/opencv/build/include/opencv;D:/opencv/build/include/opencv2
Configuring done Generating done
seems good right ? and then on the build directory from command prompt, i typed mingw32-make
and this shows up
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x72):
undefined reference to
cv::imread(cv::String const&, int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0xe3):
undefined reference to
cv::namedWindow(cv::String const&,
int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x129):
undefined reference to
cv::imshow(cv::String const&,
cv::_InputArray const&)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text+0x149):
undefined reference to
cv::waitKey(int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42):
undefined reference to
cv::String::allocate(unsigned int)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$ZN2cv6StringaSERKS0[__ZN2cv6StringaSERKS0_]+0x1c):
undefined reference to
cv::String::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to
cv::fastFree(void*)'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40):
undefined reference to
cv::Mat::deallocate()'
CMakeFiles\Display.dir/objects.a(DisplayImage.cpp.obj):DisplayImage.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4):
undefined reference to
cv::fastFree(void*)' collect2.exe:
error: ld returned 1 exit status
mingw32-make[2]: *
[CMakeFiles\Display.dir\build.make:104:
Display.exe] Error 1 mingw32-make[1]:
* [CMakeFiles\Makefile2:72: CMakeFiles/Display.dir/all] Error 2
mingw32-make: *** [Makefile:83: all]
Error 2
how do i fix this ? or is it because that i use the library straight from the website ?
i also try something like putting a wrong parameter on the imread function and then type mingw32-make and it did tell me that i use a wrong parameter. If they know i used the wrong parameter, then why they can't tell what is imread for (or why is it undefined reference ? ) ? I'm using windows.
I'm very new to these libraries, mingw, and cmake, so i'm sorry if my question is stupid.
this question is not duplicate because, i use windows and mingw32-make, everyone that asks this question did not use that and the question is in a different situation than mine.

Clion Cmake & SDL2

I want to use Clion with SDL2 and after lot of tests, I already have an error.
there is my CmakeLists.txt :
cmake_minimum_required(VERSION 3.8)
project(sdl2-test)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
#set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp)
add_executable(sdl2-test ${SOURCE_FILES})
target_link_libraries(sdl2-test SDL2main SDL2)
my main.cpp
#include <iostream>
#include "include/SDL2/SDL.h"
using namespace std;
int main(int argc, char** argv) {
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
cout << "Something went wrong! " << SDL_GetError() << endl;
}
SDL_Window* window = SDL_CreateWindow("SDL_Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, SDL_WINDOW_OPENGL);
if(window == nullptr){
cout << "Something also went wrong here" << endl;
}
SDL_Delay(2000);
SDL_Quit();
return 0;
}
but when I build my project cmake Obviously Cmake work find but I haave some error :
C:\Users\paulp\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\172.4343.16 \bin\cmake\bin\cmake.exe --build C:\Users\paulp\CLionProjects\sdl2-test\cmake- build-debug --target sdl2-test -- -j 4
[ 50%] Building CXX object CMakeFiles/sdl2-test.dir/main.cpp.obj
[100%] Linking CXX executable sdl2-test.exe
CMakeFiles\sdl2-test.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:8: undefined reference to `SDL_Init'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:9: undefined reference to `SDL_GetError'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:13: undefined reference to `SDL_CreateWindow'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:18: undefined reference to `SDL_Delay'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:19: undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\sdl2-test.dir\build.make:96: recipe for target 'sdl2-test.exe' failed
mingw32-make.exe[3]: *** [sdl2-test.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/sdl2-test.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/sdl2-test.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/sdl2-test.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/sdl2-test.dir/rule] Error 2
mingw32-make.exe: *** [sdl2-test] Error 2
Makefile:117: recipe for target 'sdl2-test' failed
any idea how to fix ?
The linking of your application has failed because the linker does not know the location of the sdl2-lib.
You need to search for the SDL2-libraries as well in your cmake-file and add the locations to your makefile:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(ChickenShooter main.cpp)
target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR})
target_link_libraries(ChickenShooter ${SDL2_LIBRARY})
You can look into this post as well: How to dins SDL2 via cmake

Linking issues using OpenSSL in Ubuntu

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.