Blowfish and undefined reference to `BF_set_key' - c++

I have installed Win64 OpenSSL v1.0.1b and Visual C++ 2008 Redistributables from this page http://slproweb.com/products/Win32OpenSSL.html and added compiler (C:\OpenSSL-Win64\include) and linker paths (C:\OpenSSL-Win64\bin, C:\OpenSSL-Win64\lib, C:\OpenSSL-Win64) to Code::Blocks, but I still cannot compile my program.
Source code:
#include <cstring>
#include <openssl/blowfish.h>
int main() {
const char * key = "aaabbbcccdddeeefffggghh";
BF_KEY bfKey;
BF_set_key(&bfKey, strlen(key), (const unsigned char *) key);
return 0;
}
Error:
obj\Release\main.o:main.cpp|| undefined reference to `BF_set_key'|
I tried to add -lssl, -lopenssl, -llibssl, -lcrypto, but it doesn't work. Then I can see another error:
ld.exe||cannot find -lssl|
I have no idea (and Google also) what to do. Any ideas what I do wrong?

I'm not sure if you configured it properly. It seems like you also have to add the libraries your project is using somewhere in Build Options, on top of setting the library directories. Does this help? http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/

Because you are using the GCC compiler (MinGW) with Code::Blocks you have to change the library sear directory (C:\OpenSSL-Win64\lib) to C:\OpenSSL-Win64\lib\MinGW and to link the library that have the Blowfish function you must use -leay32 (in your case probably is -leay64).
Inside the directory C:\OpenSSL-Win64\lib\MinGW there are 2 files with the .def extensin that have the list of functions exported by each library (libeay32.a/libeay64.a and ssleay32.a/ssleay64.a), by the way if you use the -l option the file must be called lib.a; the if you want to use any of the functions on the library ssleay32.a/ssleay64.a you must link the file directly (for example C:\OpenSSL-Win64\lib\MinGW\ssleay32.a) or append lib to the name of the file.

Related

Assistance including a basic SDK into a C++ program Dev C++

I have been making some applications that I would like to link to discord, specifically with Discord's SDK. The SDK comes with 64 and 32 bit x84 lib files, and a C++ folder full of the includes it needs, so I would assume C++ is supported.
However, I am very terrible at linking libraries or anything at that, and always run into issues when linking. I am using Dev C++ as my IDE, and my code is as follows:
#include <iostream>
#include "Discord/discord.h"
using namespace std;
void InitDiscord()
{
auto discid = 772671910668133376; //Not my actuall discord app ID, but real one does not make a difference
discord::Core* core{};
discord::Core::Create(discid, DiscordCreateFlags_Default, &core);
}
int main(){
InitDiscord();
cout << "Discord active";
while(1){
}
return 0;
}
and I am getting the error:
C:\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe Discord Testing.o:Discord Testing.cpp:(.text+0x32): undefined reference to `discord::Core::Create(long long, unsigned long long, discord::Core**)'
for only the line discord::Core::Create(discid, DiscordCreateFlags_Default, &core); and not discord::Core* core{};
I am using C++17 and a newer TDM-GCC compiler, the same one that works for all of my other applications. I am including the .lib files and .dll files in the program's directory, and in the linker the only thing I am using is -discord_game_sdk.dll.lib which is a valid directory. I have also tried discord_game_sdk.dll.lib and putting the library in the same directory as the includes: Discord/discord_game_sdk.dll.lib. I have tried using both 32 bit and 64 bit libraries in all project and compiler directories with no change, and im sure this is something probably really simple, but nowhere have I found any example C++ discord programs or how to include their SDK.
If anyone could figure out what the problem is and how I can fix it, that would be very helpful and appreciated.
EDIT:
It appears that user4581301 was right, TDM-GCC and other Mingw compilers do not support .lib files, and will ignore them despite being linked. The SDK did not come with any other formats other than .dylib, .so, and .bundle.
This creates a somewhat new issue, I already have my compiler set up and cannot really switch to Visual Studio, so I need a way to convert .lib to .a somehow. A post here recommends http://code.google.com/p/lib2a/ , which requires a .def file, another file that did not come with the SDK, but apparently a program called gendef.exe that came with my compiler can create .def files from .dll files. That is indeed the case, however when attempting it I get the error:
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin>gendef.exe discord_game_sdk.dll
* [discord_game_sdk.dll] Found PE image
* failed to create discord_game_sdk.def ...
with no other warnings. Now I need to know if I am converting wrong, if there is an easier workaround, or if one of the other file types can be converted or used. Any suggestions at this point are welcome and appreciated!

Undefined Reference when calling Win32 API Function

I am trying to use the SetCursorPos function to move my cursor in Windows 10 with C++.
Here is my code:
#include <Windows.h>
int main()
{
SetCursorPos(100,100);
return 0;
}
Whenever I run the code, I get this error:
undefined reference to `SetCursorPos#8'
I have read through What is an undefined reference/unresolved external symbol error and how do I fix it?, but I could not find a solution to my problem.
I am sure that I missed something, but I have no clue to what that something is; sorry if the answer is posted elsewhere.
Microsoft ships the SetCursorPos function inside user32 library (see MSDN)
If you use Microsoft Visual C++ compiler, you can add statically that library to your project using the name "user32.lib".
If you use GCC instead, the name has another extension: "user32.a". Normally the ".a" extension is defaulted in GCC, so no need to pass it to the compiler.
If you were to add libraries that are not in a GCC-knowing path, then you need the "-L" flag to tell GCC where to look for that libraries. The "-l" flag (lowercase L) tells GCC to use that library.
Sumarizing:
g++ movecursor.cpp -luser32

Unable to compile program with twitcurl

I want to compile a C++ program with a twitter library, on Linux.
I'm current using twitcurl as the twitter API library and installed g++ and all the necessary files and packages that are listed on the official website: http://code.google.com/p/twitcurl/wiki/WikiHowToUseTwitcurlLibrary
However, when I compile my program using this command g++ twitterClient.cpp -ltwitcurl, I get this error: cannot find -ltwitcurl
I also used CodeBlocks IDE to compile it but got this error: undefined reference to twitCurl::~twitCurl()
`
My code only contains a few lines:
#include <iostream>
#include "Twitter/Twitter.hpp"
using namespace std ;
int main ()
{
Twitter t ;
return 0 ;
}
I've already spent a lot of time on this but am unable to solve the problem. What should I do in order to compile the program on the command-line and CodeBlocks?
$ g++ twitterClient.cpp -ltwitcurl
cannot find -ltwitcurl
This means your compiler doesn't find the libtwitcurl.so.1. in its library directories.
First, make sure you correctly build the twitcurl library and obtained the libtwitcurl.so.1. file with something like this :
svn co http://twitcurl.googlecode.com/svn/trunk/libtwitcurl
cd libtwitcurl/
make
Secondly, make sure you put the file (or a symlink) in one of your compiler's library path :
cp libtwitcurl.so.1.0 /usr/lib/
You can check g++ library paths using the following command :
g++ --print-search-dirs | grep libraries
(/usr/lib/ is usually at the end.)
If you don't want/can't put the file in your compiler's library path, you can also tell it where to find libtwitcurl.so.1. by adding -L/path/to/twitcurl/ in the g++ options, but it is not needed if the file is already in one of the compiler's library path.
You need to specify path to twitter lib:
g++ twitterClient.cpp -L/path/to/lib/dir -ltwitcurl

Errors while trying to compile with external libraries

I have downloaded the mimetic library installation files,
and followed the INSTALL instructions.
./configure
a script that creates the make file after checking a series of things.
make
compiles the cpp files, after this different .o and .lo files appear in the original folder.
make install
seems to do a lot but the only thing that I seem to notice is that a mimetic directory
appears under /usr/local/include with all the header files.
than I try to compile the most simple main file possible:
(as offered in the library site : original example )
#include <mimetic/mimetic.h>
using namespace mimetic;
int main()
{
MimeEntity me;
return 0;
}
I am compiling with following command ( on CentOS 5.7, gcc version : 4.1.2 ):
g++ mimetic.cpp
The error I get:
/tmp/ccWnsteO.o: In function `main':
mimetic.cpp:(.text+0x80): undefined reference to `mimetic::MimeEntity::MimeEntity()'
mimetic.cpp:(.text+0x91): undefined reference to `mimetic::MimeEntity::~MimeEntity()'
collect2: ld returned 1 exit status
From this I understand that the header files are found but the source/library itself
is missing.
the MimeEntity constructor declaration appears in : /usr/local/include/mimetic/mimeentity.h
when I do a search for mimeentity I get the following :
/home/mimetic-0.9.7/mimetic/mimeentity.o
/home/mimetic-0.9.7/mimetic/mimeentity.h
/home/mimetic-0.9.7/mimetic/mimeentitylist.h
/home/mimetic-0.9.7/mimetic/mimeentity.cxx
/home/mimetic-0.9.7/mimetic/.libs/mimeentity.o
/home/mimetic-0.9.7/mimetic/mimeentity.lo
/home/mimetic-0.9.7/mimetic/.deps/mimeentity.Plo
/usr/local/include/mimetic/mimeentity.h
/usr/local/include/mimetic/mimeentitylist.h
I've tried with a search path to the libraries but the same error appears
g++ mimetic.cpp -L/home/mimetic-0.9.7/mimetic/
Something else strange is happening, when I try to compile the main mimetic.cpp file
with the line
MimeEntity me;
changed to
MimeEntity me();
it compiles.
You are getting a linker error simply because you are not referencing the library when compiling the test source file. It needs to be something like:
g++ mimetic.cpp -l<libraryname>
The reason it compiles when you add the braces is that you are really declaring a function called 'me' that returns a MimeEntry. While it compiles, it does not do what you want.
The command you are using to build your mimetic example seems incomplete. You are specifying library search patch (-L) but not the library itself.
Make sure that -L option specified the location of the mimetic library
Add -l'the-name-of-the-mimetic-library'. My guess would be -lmimetic
Add -I (that is capital i) option for the location of the headers.

GMP Library, compile error in C++, MinGW, Code::Blocks

I have built GMP for MinGW. The IDE I'm using is Code::Blocks. I don't have any problems with C functions of GMP. But I'm having problem with C++. The program I tried to run is as simple as this,
#include<iostream>
#include<gmpxx.h>
using namespace std;
main()
{
mpz_class a;
a=12345;
cout<<"value"<<a<<"\n";
return 0;
}
And the Errors I get are
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osmpz.o):osmpz.cc|| undefined reference to `__gmpz_get_str'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osfuns.o):osfuns.cc:(.rdata+0x1c)||undefined reference to `__gmp_asprintf_memory'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osfuns.o):osfuns.cc:(.rdata+0x20)||undefined reference to `__gmp_asprintf_reps'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osdoprnti.o):osdoprnti.cc|| undefined reference to `__gmp_doprnt_integer'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osdoprnti.o):osdoprnti.cc|| undefined reference to `__gmp_asprintf_final'|
||=== Build finished: 5 errors, 0 warnings ===|
Now, some additional data:
I don't have any problem with C functions. And also, if I remove cout<< statement the file compiles and runs fine. The problem is probably with overloaded operators.
libgmpxx.a and libgmp.a are linked with compiler. It can be seen in the error messages too...
The problem is probably with the libgmpxx.a alone. So, I built the Library again, but the files are same.
I used this tutorial build GMP with MSYS for MinGW. http://www.cs.nyu.edu/exact/core/gmp/ and http://suchideas.com/journal/2007/07/installing-gmp-on-windows/
The version of GMP I'm using is 5.0.4.
So, what I want to know is, what could be the problem? And how could it be solved?
And, if unsolvable and if you have the working files for 5.0.4 version, please share it. :(
I suspect the command to build your program specifies the libgmp* libraries in the wrong order. Make sure the libgmp.a library is specified after the libgmpxx.a library:
-lgmpxx -lgmp
If they are specified in the other order, then when pulling in dependencies from libgmpxx.a library, the libgmp.a library won't be searched.
From the ld linker's docs on the -l option:
The linker will search an archive only once, at the location where it
is specified on the command line. If the archive defines a symbol
which was undefined in some object which appeared before the archive
on the command line, the linker will include the appropriate file(s)
from the archive. However, an undefined symbol in an object appearing
later on the command line will not cause the linker to search the
archive again.
See the -( option for a way to force the linker to search archives
multiple times.
You may list the same archive multiple times on the command line.