undefined reference to '_WSAStartup#8'. How to link libraries with NetBeans? - c++

I use NetBeans, Windows and Cygwin with G++ compiler.
I'm examining Windows Sockets 2. I do everything that is written in MS manual. I have a code (mostly from this manual):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
else cout << "Initialization OK.";
return 0;
}
And I have a problem when I try to run the project:
undefined reference to `_WSAStartup#8'
I understand that Ws2_32.lib is missing. This is because I do not have Windows SDK installed. But before installing it I want to try out tools that Cygwin offers. It has all the w32api header files, I have them in:
C:\cygwin\usr\include\w32api
And it has some w32api almost .lib files in the directory:
C:\cygwin\lib\w32api
But all these lib files are different, they have .a extension and a little bit different name, like:
libws2_32.a // in Cygwin
vs.
ws2_32.lib // in Windows
When I use Cygwin terminal to create an .exe file, everything works fine. The commands I input are:
cd C:\\c++\\myProgram // go to the dir
g++ myProgram.cpp -lws2_32 // compile using -l option to link libws2_32.a
And after it I get a.exe file. I run it and it works:
./a.exe // Initialization OK.
But as I said I use NetBeans. And if I try to run the project from NB ([F6] button) I always have this error undefined reference to '_WSAStartup#8'.
I've tried already everything I could find on NB forums. I've tried to link libws2_32.a to my project this way. I go to:
File -> Project Properties -> Linker -> Libraries
And there are three options:
Add Library...
Add Library File...
Add Option...
I've tried them all. I've tried to link both just Add Library... and Add Library File.... I've also tried to add such an option in the Add Option... button:
Add Option... -> Other option -> // and I input here "-lws2_32"
But whatever I do I can't run the project from NB, I get error undefined reference to '_WSAStartup#8'.
So it seems that it is not a problem (error) in the code. It seems that the problem is with NB, with its possibility to link libraries. Or I do wrong steps to attach them to the project.
So my questions are:
1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
2) What is the difference between Windows .lib files and Cygwin .a files? Is it better to install Windows SDK and just forget about those .a files? Everything I could find so far about them on Cygwin site is this:
The import library is a regular UNIX-like .a library, but it only
contains the tiny bit of information needed to tell the OS how your
program interacts with ("imports") the dll. This information is linked
into your .exe. This is also generated by dlltool.
3) Is it possible to use #pragma comment(lib, "libws2_32.a") to link .a files? I've tried but didn't get success results.

1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
Try this: http://forums.netbeans.org/ptopic44959.html
2) What is the difference between Windows .lib files and Cygwin .a files? Is it better to install Windows SDK and just forget about those .a files?
Both of these files in this particular case are called "import libraries". Import libraries are basically a file containing a list of valid functions, so that when you link your exe, the linker knows that those functions will exist in some particular DLL. So when you link to wsock32.lib or ws2_32.lib, the linker now knows that these functions will exist in wsock32.dll and ws2_32.dll. Thus, it will not complain. Now, the .lib import library format is Microsoft's format. GCC/unix/linux/mingw/cygwin etc. have a different format, and the extension for that format is .a. Now, cygwin/mingw etc. provide a ws2_32.a so that when using cygwin/mingw/gcc, the linker can read the import library in the correct format. cygwin/mingw/gcc will simply not understand the .lib. Microsoft provides the .lib files in their SDK, but I am not sure how this will help in this case. (Though the SDK is definitely useful, because it provides lots of header files and DLLs for other useful things you might need, but the import libraries are useless, because gcc/mingw/cygwin will not understand them; unless you use a converter tool, like the one mentioned in your duplicate question).
3) Is it possible to use #pragma comment(lib, "libws2_32.a") to link .a files? I've tried but didn't get success results.
No, the #pragma linking comments are an MSVC specific (ugly IMO) extension. Use the linker options in the menus.

eclipse, Cygwin
propeties -> C/C++ Build -> Settings -> Cygwin C Linker
Command line pattern
add to -lws2_32
ex)${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lws2_32

Related

Xcode Version 6.0.1 gmp.h header file not found, even though gmp.h and gmpxx.h are in local/include

i'm trying to get GMP working with Xcode. So far I have untared the file in the home directory, ran all the necessary commands to have it configure, make, and install
./configure --prefix=/usr/local --enable-cxx
make
make check
sudo make install
Both the gmp.h and gmpxx.h are in the usr/local/include, however I still am getting an error when trying to include the files. I believe I need to add a complier flag but for this version of Xcode I do not know how to do that. If that is not the case and I need to do something else please advise and I would very much appreciate it.
My code looks like the following:
#include <iostream>
#include <gmp.h>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Can I code with GMP in c++ ? Or do I need code in c? Any help getting this to work would be very much appreciated, thanks a ton in advance!
Sorry if this seems like a DUPLICATE question, i did look at the other questions and I was not able to get it to work following answers from previous questions.
UPDATE
Added the libgmp.la and libgmpxx.la to Xcode, also added /usr/local/include to header file search and it is still not working! I see the library files in my project and if I put "gmp.h" instead of it works but when I go to use GMP it fails telling me to chnange the in other GMP files to "gmp.h" which doesn't seem right.
ANY IDEAS?
this is the fix according to another SO question:
1) Open the left panel,
goto "ProjectName", Targets, Build phases, Link binary with libraries
and select your library.
2) Open the left panel,
goto "ProjectName", Project, Header Search Paths,
write the path where the headers of your library are
(the .h files, usually in /usr/local/include).
3) Open the left panel,
goto "ProjectName", Project, Library Search Paths,
write the path where your libraries are
(the .a or .dylib files, usually in /usr/local/lib)

Import library creation, C++/VisualStudio2013

I create a set of C++ DLLs and their import libraries in VisualStudio (2013). So far, these were all generated at the same time by linker. My DLLs use symbols from other libraries, which are included as additional dependencies in properties/link edition.
Sometimes, the DLL generation fails because of missing importation libraries, which is fine to me, but in such case there's no generated .lib file either. I would like to generate the import libs (of my DLLs) even if the DLLs cannot be done, in order to allow other projects to rely on the symbols I shall export before I'm able to provide the whole stuff.
I found a workaround through a pre-link event command line, with:
LIB /DEF:(...)\myLib.def /OUT:(...)\myLib.lib $(IntDir)*.obj
This generates exactly what I need, when I do have a .def file.
The problem is, I most often don't have such a .def file but rely on __declspec(dllexport) instructions instead, and in such case I didn't find a way to get a correct result.
I tried:
LIB /OUT:(...)\myLib.lib $(IntDir)*.obj (#1)
This creates a (static ?) lib file, not an import library, not what I need.
LIB /DEF /OUT:(...)\myLib.lib $(IntDir)*.obj (#2)
This fails on error 1104 because of missing (other) import libraries.
What's more puzzling to me is, the command line (#2) fails on libraries that should be here (they are found by the linker, actually). I'm wondering whether I am supposed to provide the complete LINK command line arguments to the LIB command, in which case it would be a no-go.
I'm not much of a specialist of the compilation/link tools, I fear, I'm probably doing pretty wrong stuff...
Does anyone know of a simple way to fulfill my need without using a .def file ? Is there an option in the linker to ask for import library creation from the declared __declspec(dllexport) symbols and generate it even when the DLL cannot link ?
This fails on error 1104 because of missing (other) import libraries.
Edit: oh, I get it. .obj files have /DEFAULTLIB: inside them and lib complains. Use /NODEFAULTLIB
hasunresolved.c:
#pragma comment(lib, "missinglib.lib")
void missingfunc();
__declspec(dllexport)
void dllfunc() {
missingfunc();
}
usedll.c
__declspec(dllimport)
void dllfunc();
int main() {
dllfunc();
return 0;
}
in VS command prompt:
cl -c hasunresolved.c
lib /def /OUT:test.lib hasunresolved.obj
rem this prints: LIB : fatal error LNK1104: cannot open file 'missinglib.lib'
lib /NODEFAULTLIB /def /OUT:test.lib hasunresolved.obj
cl usedll.c test.lib
dumpbin /IMPORTS:test.dll usedll.exe
output:
Dump of file usedll.exe
test.dll
0 dllfunc

ws2_32.lib vs. libws2_32.a, what's the difference and how to link libws2_32 to NB project?

I use NetBeans, Windows and Cygwin with g++ compiler.
I'm examining Windows Sockets 2. I do everything that is written in MS manual. I have a code (mostly from this manual):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
else cout << "Initialization OK.";
return 0;
}
And I have a problem when I try to run the project:
undefined reference to `_WSAStartup#8'
I understand that Ws2_32.lib is missing. This is because I do not have Windows SDK installed. But before installing it I want to try out tools that Cygwin offers. It has all the w32api header files, I have them in:
C:\cygwin\usr\include\w32api
And it has some w32api almost .lib files in the directory:
C:\cygwin\lib\w32api
But all these lib files are different, they have .a extension and a little bit different name, like:
libws2_32.a // in Cygwin
vs.
ws2_32.lib // in Windows
When I use Cygwin terminal to create an .exe file, everything works fine. The commands I input are:
cd C:\\c++\\myProgram // go to the dir
g++ myProgram.cpp -lws2_32 // compile using -l option to link libws2_32.a
And after it I get a.exe file. I run it and it works:
./a.exe // Initialization OK.
But as I said I use NetBeans. And if I try to run the project from NB ([F6] button) I always have this error undefined reference to '_WSAStartup#8'.
I've tried already everything I could find on NB forums. I've tried to link libws2_32.a to my project this way. I go to:
File -> Project Properties -> Linker -> Libraries
And there are three options:
Add Library...
Add Library File...
Add Option...
I've tried them all. I've tried to link both just Library and Library File. I've also tried
to add such an option in the Add Option... button:
Add Option... -> Other option -> // and I input here "-lws2_32"
But whatever I do I can't run the project from NB, I get error undefined reference to '_WSAStartup#8'.
So my questions are:
1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
2) What is the difference between Windows .lib files and Cygwin .a files? Is it better to install Windows SDK and just forget about those .a files? Everything I could find so far about them on Cygwin site is this:
The import library is a regular UNIX-like .a library, but it only
contains the tiny bit of information needed to tell the OS how your
program interacts with ("imports") the dll. This information is linked
into your .exe. This is also generated by dlltool.
3) Is it possible to use #pragma comment(lib, "libws2_32.a") to link .a files? I've tried but didn't get success results.
UPD:
The answer for the 3rd question -> #pragma comment(lib, "xxx.lib") equivalent under Linux?
I had this problem Eclipse/CDT/Windows.
This is my build command
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o source\Sever_B.o ..\source\Sever_B.cpp
g++ -LC:\MinGW\lib -o Sever_B.exe source\Sever_B.o -lws2_32
So on the Eclipse project properties, C/C++ General, Paths & Symbols
Add C:\MinGW\lib to library paths tab
Add ws2_32 to libraries tab
This links libws2_32.a to my project and it now builds OK.
I tried using the Windows ws2_32.dll and ws2_32.lib and got nothing but pain.
It seems to me that you've added the -lws2_32 switch to the C/C++ compiler options. When netbeans invokes the compiler, it passes on the -c switch and it'll ignore the linker options, such as -l. In the linker options section, Netbeans has an apropriate place to add external libraries. Or you can add the -l as an extra option to the linker. That might solve the problem.
*.lib files are used by the Microsoft toolchain (cl.exe) and lib*.a are used by GNU toolchain (that's the piece of adivse you've found). If you're going to use Cygwin, you'll need the lib*.a files. In this context, having Microsoft SDK won't help you at all. Also, if you need a file that only exists in .lib format, you can convert to.a` with a tool called reimp.
Hope this helps.

C++ include libraries

Ok, so it's been a while, and i'm having problems with #includes
So I'm doing
#include "someheader.h"
but it's giving me
fatal error: someheader.h: No such file or directory
It's a system wide library I guess you could say.
I'm running arch linux and I installed the library from the repo, and I think the .h files are in /usr/include.
I could just copy all the header files into the folder my code is in but that would be a hack.
What is the "right" way to do this?
Edit: I wasn't correct by saying the .h files were in /usr/include, what I meant was that the library folder was in there
So, Emile Cormier's answer worked to a certain extent.
The problem now is that there are some include in the header file and it seems from the methods I'm trying to access that those includes are not happening
it's giving my the error
undefined reference to Namespace::Class::method()
Edit:
Ok so the final answer is:
#include <library_name/someheader.h>
And compile with
g++ code.cpp -llibrary_name
Sometimes, header files for a library are installed in /usr/include/library_name, so you have to include like this:
#include <library_name/someheader.h>
Use your file manager (or console commands) to locate the header file on your system and see if you should prefix the header's filename with a directory name.
The undefined reference error you're getting is a linker error. You're getting this error because you're not linking in libsynaptics along with your program, thus the linker cannot find the "implementation" of the libsynaptics functions you're using.
If you're compiling from the command-line with GCC, you must add the -lsynaptics option to link in the libsynaptics library. If you're using an IDE, you must find the place where you can specify libraries to link to and add synaptics. If you're using a makefile, you have to modify your list of linker flags so that it adds -lsynaptics.
Also the -L <path_to_library> flag for the search path needs to be added, so the linker can find the library, unless it's installed in one of the standard linker search paths.
See this tutorial on linking to libraries with GCC.
You'd use #include <someheader.h> for header files in system locations.
#include "someheader.h" would try to include the file someheader.h in the directory of your .c file.
In addition to including the header file, you also need to link in the library, which is done with the -l argument:
g++ -Wall youprogram.cpp -lname_of_library
Not doing so is the reason for the "undefined reference .. " linker errors.
The quick fix is to do use:
#include <someheader.h>
assuming that someheader.h is in the standard include locations (to find it use the command locate someheader.h in a shell. If it is in /usr/include it is in a standard location. If it is in a subdirectory of /usr/include you only need to add the part of the directory up to /usr/include in the #include directive (e.g. #include <fancy_lib/someheader.h>)
However, this is only half of the story. You also will need to set up your build system in a way that locates the given library and adds its include path (the path under which it's header files are stored) to the compiler command (for gcc that is -I/path/to/header). That way you can also build with different versions by configuring them in your build system. If the library is not header-only you will also have to add it to the linker dependencies. How this is achieved in your build system is best found out by consulting its documentation.

Problems including jsonCpp headers

I'm trying to implement the jsoncpp libraries in my C++ code, I wrote a simple piece of code just to try it out, and it's not even compiling.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifndef json_included
#define json_included
#include "jsoncpp\include\json\json.h"
#endif
//#include "json\jsonC\json.h"
int main(int argc, char **argv)
{
std::string example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
Json::Value value;
Json::Reader reader;
bool parsed = reader.parse(example, value, false);
std::cout << parsed;
return 0;
}
The errors i'm getting are:
undefined reference to `Json::Reader::parse(std::string const&, Json::Value&, bool)'
undefined reference to `Json::Reader::Reader()'
undefined reference to `Json::Value::~Value()'
undefined reference to `Json::Value::Value(Json::ValueType)'
I'm a bit new to C++, is there something I'm missing in the include statement? Or does jsonCpp need something extra?
Thank you for your time!
Your code is compiling, but it is not linking. You forgot to provide the JSON shared library files to your linker (or, on newer versions, to add the amalgamated jsoncpp.cpp to your project).
Without knowing more about your development environment, it's hard to give you more specific instructions.
BTW, you're writing C++; use C++ headers like cstdio, not stdio.h, please. You also failed to include C++ string and got lucky that it "worked" through some JSON header including it for you.
"Undefined reference" sounds like a linker problem. Does jsoncpp come with a library that you need to link to, such as a .so, .a, .lib or .dll file?
According to the jsoncpp README, the library must first be built using scons. Presumably this will then output a library file such as a .so, .a, .lib or .dll file. You must then follow your compiler's rule for linking against such a library (e.g. add it to the end of the command line when compiling, or add it to the "additional libraries" field in the project config in your IDE).
In my case (using CodeBlocks IDE on Ubuntu) the problem was that I needed to add the json.cpp file (generated with python amalgamate.py from within the jsoncpp project) to my build targets.
In other words, I added a -c jsoncpp.cpp option to my g++ compile statement.
You need to link to the json libraries, e.g. using -ljson_linux-gcc-4.4.3_libmt
You can find the exact library name by looking in the library directory, e.g. /usr/lib
If you're using Visual Studio, add the .lib file to Project Properties, Linker, Input, Additional Dependencies and specify the path in Project Properties, Linker, General, Additional Library Directories
Two potential issues:
There is a bug in some versions of the jsoncpp library code where amalgated needs to become amalgamation for the linking to work correctly.
As the other answers suggested, #include
After you compile jsoncpp you can find the libraries in the folder libs/ . For convenience you can put it in /usr/lib and then link it at run time by passing -llibjson_linux-gcc-4.4.3_libmt as an argument to g++.
I have renamed libjson_linux-gcc-4.4.3_libmt.so to libjson.so and can link it by specifying -ljson.