Library connection in GCC - c++

I have 4 files:
main.cpp
emcd.h
EMCB.dll
EMCB.lib
I want to use the functions that are in the dll.
There is not a lot of code, but it captures the essence.
#pragma comment(lib,"EMCB.lib")
#include "emcb.h"
int main()
{
EMCBLibInitialize(); //DLL function
}
To compile, I use gcc and do it with the following command
gcc main.cpp -l: EMCB.lib
after which I get an error
/usr/bin/ld: cannot find -l:EMCB.lib
collect2: error: ld returned 1 exit status

The preprocessor statement:
#pragma comment(lib,"xxx.lib")
will not be recognized by the GCC compiler. You need to use -l

Related

How to link/include c++ libraries correctly

I tried to compile program with https://github.com/yhirose/cpp-httplib "one-header" library. I wrote
#include "httplib.h"
/* example code from main github page */
and when i tried to compile program
g++ -std=c++11 -o test test.cc
i got this error:
/usr/bin/ld: /tmp/ccYMj4l8.o: in function `std::thread::thread<httplib::ThreadPool::worker, , void>(httplib::ThreadPool::worker&&)':
test.cc:(.text._ZNSt6threadC2IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_[_ZNSt6threadC5IN7httplib10ThreadPool6workerEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
What can i do?
And how to link libraries that have include and src directories, e.g. libcurl
It's gcc's known particular feature, its std::thread implementation is built upon pthreads so it requires specifying -pthread to correctly link programs with threads.

Custom (musl) Build of GCC Cannot Find LD

I followed this 'tutorial' to create a static GCC, libc, and libstdc++ built against MUSL.
Build directory: /home/user/musl_gcc/
When I attempt to create a simple test C program, and compile it with no arguments, I get the following error:
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ cat test.c
#include <stdio.h>
#include <unistd.h>
int main()
{
puts("Test");
_exit(0); // return 0 leads to a segfault (unsure why)
}
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ ./amd64-linux-musl-gcc test.c
collect2: fatal error: cannot find 'ld'
compilation terminated.
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ which ld
/usr/bin/ld
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ `!!` -v
`which ld` -v
GNU ld (GNU Binutils) 2.29.1
The linker does NOT point to the custom built one, nor would I expect it to.
This can be rather easily circumvented by compiling and linking in separate steps:
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ ./amd64-linux-musl-gcc test.c -c
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ ./amd64-linux-musl-ld test.o -L../lib/ -lc -o test
./amd64-linux-musl-ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$ ./test
Test
[user#Arch]: ~/musl_gcc/amd64-linux-musl/bin>$
However. this is of little use to me. I'd eventually like to get it to work with C++ applications that use CMake as a build environment, but this has been a tedious cat-and-mouse game of "find the library." How do I specify the linker to use? I thought it was though the LD environment variable, but that proved fruitless.

Bullet basic project error: undefined reference to `btTypedConstraint::serialize(void*, btSerializer*) const'

I have been trying to link bullet to an existing project. I was able to compile and install the source and the example programs, but I can't compile a basic empty file.
here is the test.cpp code:
#include "btBulletDynamicsCommon.h"
#include <stdio.h>
int main()
{
return 0;
}
and the error:
g++ -std=c++11 -o run test.cpp -I /usr/local/include/bullet/ /usr/local/lib/libBullet3Dynamics.a /usr/local/lib/libBulletCollision.a /usr/local/lib/libLinearMath.a
/tmp/ccvDsyTQ.o:(.rodata._ZTV17btTypedConstraint[_ZTV17btTypedConstraint]+0x60): undefined reference to `btTypedConstraint::serialize(void*, btSerializer*) const'
collect2: error: ld returned 1 exit status
Compilation failed.
The btBulletDynamicsCommon.h header includes the 2.x part of the library, which requires libBulletDynamics.a. Version 3 (libBullet3Dynamics.a) is incomplete and not recommended for use.

linking error in boost asio

I am getting below linking error while compiling a simple piece of code using pthread.
The system i am working on is x86_64 (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) and am compiling as -
gcc -lpthread ~/temp/temp.cpp -lrt -L"/usr/lib/x86_64-redhat-linux5E/lib64/".
Its evident that the problem is with pthread library. So i have two questions.
Q1. is it fine to use x64 pthread libs with x86 code. (In this piece of code it doesnt matter but it does in my usage) (I guess its not but how might i differentiate between x64 and x86 libs?)
Q2. i see pthread and lrt both are present at /usr/lib but even if i give that path it says -lpthread not found.
What am i missing here? Thanks for your help.
Error :
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/tmp/cc2GQOUf.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Code :
#include <stdio.h>
#include <pthread.h>
int main()
{
pthread_t f1_thread;
return 0;
}
In compilation phase,compiler can use gcc or g++,but g++ will call gcc command automatically.But in link phase,linker can use g++ or gcc -lstdc++.Because the gcc command cannot link with the libraries that be used by c++ program automatically, so usually use g++to complete the link.
So the command g++ -g -lpthread ~/temp/temp.cpp -o temp should be ok.

Undefined reference to __dso_handle_ - compiling C++ on cygwin

I have a basic Hello World C++ program that I am trying to compile and run on cygwin with g++. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world." << endl;
return 0;
}
Compiling it with: g++ helloWorld.cpp -o h results in the following error:
/tmp/ccDO1S4J.o:helloWorld.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): undefined reference to__dso_handle'
collect2: error: ld returned 1 exit status`
I have been reading up on some other threads that indicate that it might be a linker problem and that invoking the linker separately or with verbose output might lead to some more clues.
So, I did:
1. g++ -c helloWorld.cpp -o helloWorld.o (this works - no errors).
2. ld -o h helloWorld.o causes a lot of undefined reference to __main or std::cout etc. errors.
I think this is a linking issue and that I need to link another library perhaps. Any pointers on how to solve this are most welcome.
Re-installing g++ via the installer application on cygwin worked.