Trouble importing dll library into CodeBlocks Linker - c++

I am trying to use libcurl for a simple application using CodeBlocks IDE. In Codeblocks IDE, after clicking on Build Options ==> Linker Settings ==> Link Libraries ==> "Add" , the file browser only allows me to choose between *.a, *.so, *.lib, and *.dyl files. Why is it not allowing me to choose *.dll files? I downloaded the binary packages for Windows for libcurl and they all provide .dll files. This is what it looks like:
====UPDATE====
Hi I have now downloaded the following zip file for lib curl
which includes CURL Source, DLL Files, and a .lib file. It can be
found here: http://www.confusedbycode.com/curl/curl-7.34.0-win64.zip
However, I am still having issues being able to compile my source
code.
Below is my source code:
include
#include <iostream>
#include <stdio.h>
#include "curl/curl.h"
using namespace std;
int main()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Below is a screen shot of my CodeBlocks IDE working directory:
Below are screen shots of my build options for Linker/Compiler, and Linker Libraries:
I am not sure what is wrong with the setup. It is having trouble building. It is returning the following error messages:
Below is the latest Build Log:
-------------- Build: Debug in libcurl_c (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -Wall -g -IC:\Users\bbb\Desktop\libcurl_packages\confused_by_code\curl-7.34.0-win64\curl-7.34.0-win64\include -c C:\Users\bbb\Desktop\workspace\libcurl_c\main.c -o obj\Debug\main.o
mingw32-g++.exe -o bin\Debug\libcurl_c.exe obj\Debug\main.o C:\Users\bbb\Desktop\libcurl_packages\confused_by_code\curl-7.34.0-win64\curl-7.34.0-win64\lib\libcurl.lib
obj\Debug\main.o: In function `main':
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:9: undefined reference to `_imp__curl_easy_init'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:11: undefined reference to `_imp__curl_easy_setopt'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:13: undefined reference to `_imp__curl_easy_setopt'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:16: undefined reference to `_imp__curl_easy_perform'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:19: undefined reference to `_imp__curl_easy_strerror'
C:/Users/bbb/Desktop/workspace/libcurl_c/main.c:23: undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
6 errors, 0 warnings (0 minutes, 1 seconds)

A DLL (dynamic link library) is searched for and loaded by your program at runtime. To
make this happen automatically, you do not link the .dll itself (you can't). You link the matching import library, with extension .lib.
The import library for libcurl.dll is libcurl.lib. If you have downloaded and extracted cURL, say, into C:\develop\curl-7.34.0-win32, then you will find the import library at
C:\develop\curl-7.34.0-win32\lib\libcurl.lib. You should add this file to the libraries
for your Code::Blocks project.
The project will then link (unless it has other problems), but in order for it to run successfully it will have to find libcurl.dll in one of the locations that are searched for DLLs at runtime. The simplest way to ensure this is to place a copy of libcurl.dll in the directory from which your program runs. Otherwise you can decide on a location for it by studying
Dynamic-Link Library Search Order
You may have difficulty in finding the right binary package to download from
the plethora available here. Some
of them are packages of the cURL commandline tools (which you don't want)
and some of them are packages of the development binaries (which you do want)
for various platforms. Visit http://www.confusedbycode.com/curl/ and download
either curl-7.34.0-win32.zip or curl-7.34.0-win64.zip, depending on
whether you are targetting win32 or win64. Extract the archive and
find the import library and DLL in the subdirectories lib and dlls,
respectively.
Update for OP's further issues
Your program is the supplied example simple.c with the addition of a C++ header, <iostream>.
Delete your libcurl project and start again with a clean C project (not C++).
Add only 1 source file to the C project, the example simple.c or a copy of it. Don't make it a .cpp file or otherwise change it. Don't add any other files to the project.
In Build options -> Linker settings -> Link libraries add the relative path to libcurl.lib as you did before.
In Build options -> Search Directories -> Compiler (not Linker) add the relative path to the cURL include directory and nothing else (not the include\curl directory).
Do not put anything in Search directories -> Linker.
Build the project. It compiles and links for me.
Update #2
The problem now is that you are attempting to link the 64-bit curl-7.34.0-win64\lib\libcurl.lib with the 32-bit object code generated by your 32-bit toolchain, mingw32. You can't do that.
Replace your install of curl-7.34.0-win64 with curl-7.34.0-win32 from the same site. In your project, replace your\path\to\curl-7.34.0-win64\lib\libcurl.lib with your\path\to\curl-7.34.0-win32\lib\libcurl.lib and try again. The example will compile and link.
It will also run correctly, provided that it finds 32-bit libcurl.dll at runtime, and likewise the 32-bit DLLs that are dynamically loaded by libcurl.dll in turn. For the purpose of the example just copy all the DLLs from your\path\to\curl-7.34.0-win32\dlls into the same directory as the .exe. For regular development of cURL apps you would want the cURL libraries installed on the the system.
Since you chose to download 64-bit cURL in the first place, you may wish to build 64-bit
executables (although 32-bit executables will run on 64-bit hosts). You can't do that that
with your 32-bit toolchain, mingw32. You can install a 64-bit toolchain, e.g. TDM-GCC MinGW Compiler,
and configure it as an additional toolchain in C::B. Alternatively you could replace your
C::B installation with a C::B 13.12 one that has TDM-GCC pre-configured, from Sourceforge

Related

Adding my method to OpenCV

I want to add new method in OpenCV library. I made my_funct.cpp whose code is as simple as:
#include "precomp.hpp"
#include <stdio.h>
void cv::my_funct(){
printf("%s\n","Hello world!");
}
and I added header CV_EXPORTS_W void my_funct(); to files C:\opencv\build\include\opencv2\imgproc\imgproc.hpp and C:\opencv\sources\modules\imgproc\include\opencv2\imgproc\imgproc.hpp. Then I used CMake to build new binaries for whole library, but when I make a new project in which I use my_funct() I get an error:
The procedure entry point _ZN2cv8my_functEv could not be located in
the dynamic link library path_to_this_project\project.exe.
Other opencv functions work just fine. I'm using mingw32 to compile library and the version of OpenCV is 2.4.9. Can you tell me what am I doing wrong?
This looks like an MinGW run-time error. So going by the assumption that you didn't get any compiler or linker errors while building project.exe, your executable most likely doesn't find the matching .dll to your .dll.a import library (which must have included the my_funct() definition).
I would recommend during developments phase - not talking about the install() scripting - to add a post-build step using add_custom_command() and generator expressions to copy the right DLL next to your project.exe:
add_custom_command(
TARGET project
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"<... path to matching DLL ...>"
"$<TARGET_FILE_DIR:project>"
)
Certainly you could also let CMake find the matching DLL, but before I could go into details there I would need to see your project.exe CMake script.
Maybe also good idea - if you are in the process of extending OpenCV code - would be to use ExternalProject_Add() to include OpenCV into your project.
References
MinGW-w64 - for 32 and 64 bit Windows - Wiki: Procedure entry point OpenProcessToken? could not be located in the dynamic link library kernel32.dll
MinGW "The procedure entry point libiconv could not be located ..."
Getting started with OpenCV 2.4 and MinGW on Windows 7

Error of linking a static lib embedded with Lua on Linux

Update
after using -ldl and link liblua.a in Project 2(console) the compile is okay, but when it runs it crashed with error Segment fault (core dumped) as soon as it runs a Lua function.
Problem Background:
OS: Linux Ubuntu
IDE: CodeBlock
Launguage: C++
2 project:
Project 1: static lib using Lua;
Project 2: console application, using the lib generated by Project 1
Problem description
Project 1 (static lib) is built successfully.
The problem is that when building project 2, it says that those Lua functions in the lib from project 1 are undefined, here is part of the error messages:
g++ -o bin/Release/BattleConsoleCB obj/Release/main.o -s
../BattleConsole/libBattleCore.a
../BattleConsole/libBattleCore.a(DataLoader.o):
In function `boolDataLoader::GetNumber<double>(char const*, double&) [clone .isra.5]':
DataLoader.cpp:(.text+0x13): undefined reference to `lua_settop'
DataLoader.cpp:(.text+0x1e): undefined reference to `lua_getglobal'
DataLoader.cpp:(.text+0x2b): undefined reference to `lua_isnumber'
DataLoader.cpp:(.text+0x3e): undefined reference to `lua_tonumberx'
DataLoader.cpp:(.text+0x51): undefined reference to `lua_settop'
Note that "DataLoader.cpp" is from project 1, which should have been built in the static lib "libBattleCore.a" which should have been embedded with Lua.
How to solve the problem? Thanks for your help.
Additional information:
Project 2 should include: "libBattleCore.h", "main.cpp", libBattleCore.a
Project 1 : CodeBlockbuilding options have included "Lua/install/include" in Compile search directory and "Lua/install/lib" in Link search directory
The same code is successfully built and run on Win with VS2012
If anything else is needed, please inform, I will add it.
I am a green hand on linux and g++. :-(
Thank you
I can't comment without 50 rep. There is no support for anonymous replies. So posting here is all I can do.
It's been a few years, but with g++, I thought it was .o files that you listed on the command line and with libraries you used -L to specify the directory to search and then -lname to include the library. (Where name lacked the leading lib and trailing .a.) E.g. -L../BattleConsole -lBattleCore
Oh, and the ordering of -lfoo -lbar vs -lbar -lfoo is critical, determining which library can use code from the other.
It's not clear that a static library will necessary include code from other libraries. You may need to link (in the right order) against the libraries from project1.
Try "nm". As in nm --demangle libBattleCore.a | grep ... This will let you see what precisely is included in each library. man nm or http://linux.die.net/man/1/nm can help you figure out what the letters stand for. For example, U is Undefined (used by not provided). T is defined in the text (code) segment. W and V are weakly-defined. Etc.

OpenGL with Eclipse CDT + MinGW + GLEW + GLFW: Undefined References

Edit: I have in the meantime figured this out and written a detailed answer below.
I just tried switching from the Express version of MSVC 10 to Eclipse CDT on Win7, and while configuring I encountered a problem with the following simple OpenGL code (which works fine in Visual Studio):
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow* w;
if (!glfwInit())
return -1;
w = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!w)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(w);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
return -1;
}
while (!glfwWindowShouldClose(w))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(w);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
In Visual Studio, I include the library paths for GLFW and GLEW, and link (in addition to the stuff that VS does built-in) opengl32.lib, glew32s.lib, glfw3.lib, in that order.
Now if I do the same in Eclipse CDT, I can't get it to work. The following errors occur:
Info: Internal Builder is used for build
g++ "-LD:\\lib\\cpp\\glfw-3.0.1.bin.WIN32\\lib-mingw" "-LD:\\lib\\cpp\\glew-1.10.0binaries\\lib\\Release\\Win32" -o glfwcheck.exe main.o -lopengl32 -lglew32s -lglfw3
Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_2]+0x4): undefined reference to `_imp__wglGetProcAddress#4'
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_3]+0x4): undefined reference to `_imp__wglGetProcAddress#4'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj): bad reloc address 0x4 in section `.text[__glewInit_GL_VERSION_1_3]'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Of course I tried changing the order of the three libraries, but that only mad it worse. (By the way: I think it's strange that I can't reproduce these errors above such that they're the only ones when I re-arrange the libs to the initial order. I stay at 20+ errors until I delete ALL the libs, build, and add them again.)
After fumbling around and looking through forum posts, I figured that maybe it's a problem with the GLEW binaries, and compiled them on my own with MinGW. Only this time, I get all the 'not found' errors. I don't know how I can link statically with my self-compiled GLEW, since there's now no glew32s.lib any longer, but only libglew32.a and libglew32mx.a. Linking dynamically with glew32 and putting the dll into my project folder didn't work either.
I feel like I'm doing something very wrong here in Eclipse, or just forget some additional libraries (although I once tried putting all the ones VS uses in there as well and it still did the exact same thing).
Can you help me out? :) Otherwise I think I'd have to stay with VS, or switch to Linux + make.
I figured it out, and for anyone ever encountering the issue I will try and make clear what exactly I did to finally be able to create OpenGL projects with the setup MinGW + GLEW + GLFW. In my case, I used Eclipse CDT as IDE, but I'll write down the resulting g++ command line so it should be easy to adapt to other IDE's.
I'll suppose MinGW and MSYS (can be chosen to be installed from within the MinGW GUI; thus no need to download separately) are installed.
Download GLFW and unzip it in your external libraries folder of choice (in my case, this is D:\external\cpp, so it would be something along the lines of D:\external\cpp\glfw, where I renamed the glfw-3.0.3.bin.WIN32 folder to simply glfw).
Download the GLEW source as a zip folder an unzip it, in my case it's in D:\external\cpp\glew. Now start MSYS, cd to the glew folder and invoke make all.
Step 3 should have created (among others) the files libglew32.a and glew32.dll inside the folder glew\lib. Now right click your Eclipse CDT C++ project, go to Properties - C/C++ General - Paths and Symbols. In the Includes tab, add the paths to the include folders of GLFW and GLEW. Again, for me this is D:\external\cpp\glew\include and analogous for GLFW. In Library Paths, do the same for the folders lib (GLEW) and lib-mingw (GLFW).
Now we have to add the libraries we want our project to be linked with. If you wish to link with GLEW dynamically, make sure to include the glew32.dll in the folder where your executable will be. In Eclipse CDT, that's usually the Debug (or Release) folder in your project structure. In the Libraries tab in the options window we opened before, add (the order is important!) glfw3, glew32, opengl32, glu32, gdi32. Now building the project should work hopefully. In case you want to link statically with GLEW, add the same libraries with the exception of glew32. Instead, in the project properties go to C/C++ Build - Settings and in the Tool Settings - MinGW C++ Linker - Miscellaneous tab add the path to libglew32.a to the Other objects field. In my case, this is D:\external\cpp\glew\lib\libglew32.a. Now in order for the static linking to work, you have to either add #define GLEW_STATIC above #include <GL/glew.h> or use the preprocessor command -DGLEW_STATIC. The GLEW homepage says that it's also possible to include the glew.c and glew.h files into your project in order to link statically, but somehow this didn't really work out for me.
These steps worked for me, and they produced command lines similar to the following (I have only one file named main.cpp and used static linking with GLEW), which could be useful if you're trying to figure this matter out without Eclipse CDT.
g++ -ID:\external\cpp\glew\include -ID:\external\cpp\glfw\include -c -o main.o main.cpp
g++ -LD:\external\cpp\glew\lib -LD:\external\cpp\glfw\lib-mingw -o minimalexample.exe main.o D:\external\cpp\glew\lib\libglew32.a -lglfw3 -lopengl32 -lglu32 -lgdi32
In the dynamic linking case, simply remove the part containing libglew32.a in the second line, and add -lglew32 between -lglfw3 and -lopengl32. As a little example source file, you could just use the code in my above question.
I hope I can help anyone with this, as I sure saw me having a lot of trouble figuring this out between tens of error messages of unresolved symbols and various other problems :-)
Update: I tried to go over this again some days ago and ran into problems with the pre-compiled GLFW binaries for Windows (I'm now using Win8.1). But you can just use CMake in combination with mingw32-make to compile it on your own. Also, GLEW seems to not be getting updates anymore, so I switched to glad instead. It's also possible to use MinGW-w64 to compile the libraries and your final project as 64-bit application.

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

undefined reference to `forkpty'

So I'm developing my project in Eclipse in Ubuntu 10.04. I have the following lines of code:
#include <pty.h>
pid_t pid;
int master;
pid = forkpty(&master, NULL, NULL, NULL);
But when I try to build it within Eclipse, I get the error:
undefined reference to 'forkpty'
Any idea how to solve this problem?
You need -lutil command line argument (to use libutil shared library).
For Eclipse:
http://zetcode.com/articles/eclipsecdevelopment/
Select Project Properties. Expand the C/C++ Build tab. Select settings. From the Tool Settings tab, expand the GCC C Linker option. Click on libraries. Add the /usr/lib/libutil.so to the Libraries window. Notice, that this path may be different on your system.
That's a linking error; you're missing the util library. Do this to build on the command line:
g++ myprogram.cpp -lutil
Eclipse should have project-level settings for listing the libraries to link against.
I came upon this issue when I was trying to install delegate
So if you are trying to make delegate and getting error
undefined reference to `forkpty'
so edit _-forkpty.c file inside maker folder
vim maker/_-forkpty.c and add
#include <util.h>
make clean
make