Compiling C++: undefined reference - c++

I'm pretty new to C++ and I'm trying to compile my code. The command I'm using is g++ -o main --std=c++11 main.cpp channel.cpp. However I'm getting the following error message:
/tmp/ccLuJs81.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `gsc::Channel<int>::Channel()'
main.cpp:(.text+0x3a): undefined reference to `gsc::Channel<int>::put(int)'
main.cpp:(.text+0x4e): undefined reference to `gsc::Channel<int>::get(bool)'
collect2: error: ld returned 1 exit status
Does anyone know what's going on here? Thanks a lot!

Seems you declared a template in a header and defined it in a C++ file. This won't work. If you don't define your template in a header, you need to explicitly instantiate it in your C++ file, e.g. ,using
template class gcs::Channel<int>;
after the definition of all the methods.

Related

undefined reference to FOLDERID_.. - C++

compiling c++ from linux to windows using g++ and got an error.
Error:
undefined reference to `FOLDERID_RoamingTiles'
collect2: error: ld returned 1 exit status
I'm guessing there is a library I have to reference but I don't know what the name is.
also if that's the case what are the ways I could find the name of the library I have to reference, if that happens again in the future?
thanks.
I'm compiling under msys2/gcc. linking to uuid helped me.
g++ -luuid -o ....

Undefined reference to nlopt_... symbols

I am fairly new to Fortran and this may sound like a silly question. I encounter an error when compiling the Fortran code that is posted as an example in the NLOPT Wiki.
I compile in Ubuntu 18.04 LTS using the following command:
gfortran example-nlopt.f90 -o example-nlopt -I/usr/local/include/
The following error is produced in the terminal:
/tmp/ccbAim6b.o: In function `MAIN__':
example-nlopt.f90:(.text+0x26): undefined reference to `nlo_create_'
example-nlopt.f90:(.text+0x42): undefined reference to `nlo_get_lower_bounds_'
example-nlopt.f90:(.text+0x67): undefined reference to `nlo_set_lower_bounds_'
example-nlopt.f90:(.text+0x8a): undefined reference to `nlo_set_min_objective_'
example-nlopt.f90:(.text+0xca): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x10e): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x12d): undefined reference to `nlo_set_xtol_rel_'
example-nlopt.f90:(.text+0x164): undefined reference to `nlo_optimize_'
example-nlopt.f90:(.text+0x305): undefined reference to `nlo_destroy_'
collect2: error: ld returned 1 exit status
Based on what I saw in nlopt's documentation (https://nlopt.readthedocs.io/en/latest/NLopt_Installation/#changing-the-installation-directory) it looks like you just need to specify the library to link against. Maybe try this:
gfortran -I/usr/local/include/ -L/usr/local/lib example-nlopt.f90 -o example-nlopt -lnlopt -lm
This assumes you have the libnlopt.so in /usr/local/lib, if not then point to its location with the -L option.

Eerie Error while linking files c++

Help!
I have three classes; Point, Neuron, and CumulativeNeuron. And I have source file neurones.cpp.
when I link my files it says:
~/Desktop/assign-8$ g++ point.o neuron.o cumulativeNeuron.o neurones.o
cumulativeNeuron.o:(.rodata._ZTI16CumulativeNeuron[_ZTI16CumulativeNeuron]+0x10): undefined reference to `typeinfo for Neuron'
collect2: error: ld returned 1 exit status
I can't understand this error! please help
Thanks.
You have forgotten to implement (or perhaps to link) the first virtual function in the Neuron class. Several compilers generate the RTTI type info for a class at the point where the first virtual function is implemented. If you forget to implement that function, no type info is generated and linking fails.

with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

I am new to posix thread library, and I tried to compile a sample code from a tutorial with:
g++ -lpthread agreement.cpp -o agreement
however I was not able to compile the code and got the following error message:
a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1
I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the "proper" pthread library?
Thanks for your help!
You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt

Problems compiling an external library on linux

So I am trying to compile the libssh2 library on linux, but when I try to compile the
example it comes up with a lot of errors, and even though I include the headerfile it asks for, it still asks for it.
Here are the error messages and the resulting messages:
~/ gcc -include /home/Roosevelt/libssh2-1.2.5/src/libssh2_config.h -o lolbaise /home/Roosevelt/libssh2-1.2.5/example/scp.c
/home/Roosevelt/libssh2-1.2.5/example/scp.c:7:28: error: libssh2_config.h: No such file or directory
/home/Roosevelt/libssh2-1.2.5/example/scp.c: In function 'main':
/home/Roosevelt/libssh2-1.2.5/example/scp.c:39: error: storage size of 'sin' isn't known
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: 'AF_INET' undeclared (first use in this function)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: (Each undeclared identifier is reported only once
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: for each function it appears in.)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:81: error: 'SOCK_STREAM' undeclared (first use in this function)
/home/Roosevelt/libssh2-1.2.5/example/scp.c:87: error: invalid application of 'sizeof' to incomplete type 'struct sockaddr_in'
Here is the new errors:
scp.c:(.text+0x106): undefined reference to `libssh2_init'
scp.c:(.text+0x1fe): undefined reference to `libssh2_session_init_ex'
scp.c:(.text+0x234): undefined reference to `libssh2_session_startup'
scp.c:(.text+0x288): undefined reference to `libssh2_hostkey_hash'
scp.c:(.text+0x36f): undefined reference to `libssh2_userauth_password_ex'
scp.c:(.text+0x3e7): undefined reference to `libssh2_userauth_publickey_fromfile_ex'
scp.c:(.text+0x437): undefined reference to `libssh2_scp_recv'
scp.c:(.text+0x531): undefined reference to `libssh2_channel_read_ex'
scp.c:(.text+0x5f8): undefined reference to `libssh2_channel_free'
scp.c:(.text+0x628): undefined reference to `libssh2_session_disconnect_ex'
scp.c:(.text+0x636): undefined reference to `libssh2_session_free'
scp.c:(.text+0x66e): undefined reference to `libssh2_exit'
collect2: ld returned 1 exit status
The header file is not included: libssh2_config.h
There is an inclusion directive in the source code, so you have to indicate the path to the header with the -I option: gcc -I/home/Roosevelt/libssh2-1.2.5/src
The -include option shall be used to include a header file which is not explicitly included in the source code with a #include directive.
I just cloned the current git build and went into the 1.2.5 relase but I'm unable to reproduce your problem.
./buildconf
./configure
make
works fine. What are you trying to do exactly?