Cannot compile with boost? [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
Attempting to create a simple WebSocket c++ application, the library I am using relies on boost.
I have attempted to add boost to my project but it refuses to compile, heres my cpp file
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
main()
{
printf("Hello World\n");
return 0;
}
I am attemting to compile it with g++ -I. -Iboost_1_58_0 /home/cabox/workspace/main.cpp -o /home/cabox/workspace/bin/exe, but all i get is
In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0xfba): undefined reference to`boost::system::generic_category()'
My folder layout is just my Main.cpp file, the boost_1_58_0 & the websocketpp folders all being in the same parent directory, what is it I'm doing wrong?

Add
-lboost_system
(see also What is an undefined reference/unresolved external symbol error and how do I fix it?)
Optionally specify the library directory with -L
Make sure it's after the cpps
Undefined reference to boost::system::generic_category despite linking with boost_system

Related

gcc can't find reference to function DoIt() when linking [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am currently becoming crazy. It seems like there is a problem with gcc and it can't open include files can't find the function DoIt() when linking. I tried compiling this code in code blocks and it didn't work so I tried it with G++ in the console and it still didn't work. So I think it's a problem with gcc.
This is my code
main.cpp
#include <iostream>
#include "source.h"
int main()
{
std::cout<<"That works"<<std::endl;
DoIt();
while(true)
{
}
return 0;
}
source.cpp
#include "source.h"
#include <iostream>
void DoIt()
{
std::cout<<"That works too"<<std::endl; //Currently doesn't work
}
source.h
void DoIt();
And this is what I wrote in the terminal
g++ main.cpp -std=c++11 -o result
This is the error message when i run it
/tmp/ccG6X4Bw.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `DoIt()'
collect2: error: ld returned 1 exit status
I have no clue why it doesn't work
By default, gcc will try to compile and link. Linking without the code from source.cpp will cause the linker to be unable to link the call of DoIt to its code. If you just wish to compile, pass -c to gcc.
From the man page:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.
...
-c Compile or assemble the source files, but do not link.
The linking stage simply is not done.

C++ linking <limits> library with g++ [duplicate]

This question already has answers here:
Include header files using command line option?
(3 answers)
Closed 4 years ago.
I included these libraries into my source file main.cpp:
#include <iostream>
#include <climits>
My entire code written in Visual Studio 2017, Windows.
I transferred my code file to Linux.
When I try to compile with g++ Item.cpp main.cpp -o main , it gives an error for this part:
items[size - 1]->points = std::numeric_limits<int>::min();
So errors below:
main.cpp: In function ‘void insert_p(Item**, int, int&)’:
main.cpp:287:33: error: ‘numeric_limits’ is not a member of ‘std’
items[size - 1]->points = std::numeric_limits<int>::min();
^~~~~~~~~~~~~~
main.cpp:287:48: error: expected primary-expression before ‘int’
items[size - 1]->points = std::numeric_limits<int>::min();
^~~
A correct solution would be just replacing #include <climits>
with #include <limits>.
Unfortunately, here is the challenge: Is it possible to solve this issue without changing my source code?
Is there any compiling trick to fix this?
I'm looking for a command like g++ Item.cpp main.cpp -o main -llimits to link <limits> library.
[EDIT]:
Solved my problem with command below:
g++ Item.cpp main.cpp -o main -include "limits"
Check this: https://stackoverflow.com/a/3387518/7977464
And as #user4581301 said:
It's far better to fix the code.
Thank you all.
An easy solution would be just replacing #include <climits> with #include <limits>.
That's not an "easy" solution, it's a "correct" solution. Your source code is wrong - fix it instead of using weird workarounds.
Is there any compiling trick to fix this?
You could try to redefine the token climits to limits, but it will probably not work as it will break code trying to use climits.

Clion undefined reference to function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have a simple program with my main.cpp, a header file func.h and another source file func.cpp. I use CLion 2016.3. My compiler is gcc.
They look like this:
Main.cpp
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include "func.h"
int main() {
int c;
c = number(2);
printf("%i", c);
}
func.cpp
int number(int a){
return a;
}
func.h
#ifndef TEST2_FUNC_H
#define TEST2_FUNC_H
int number(int a);
#endif //TEST2_FUNC_H
My cmakelists.txt
cmake_minimum_required(VERSION 3.6)
project(test2)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(test2 ${SOURCE_FILES})
If i run the build i get the following error:
CMakeFiles\test2.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/name/ClionProjects/test2/main.cpp:8: undefined reference to `number(int)'
....
How can i fix this? I've searched for other similar issues and found some solutions but they didn't work for me or i didn't know what to do. Actually I have this problem with a C-Project but the issue is the same and I think the solution will be the same.
Can you please help me?
Thank you very much.
Make sure you include func.h in your main
#include "func.h"
and put 'func.cpp' in CMakeList.txt set source
List the sources explicitly here. That is all (.cpp, .c ,.cc) to compile together. If the sources files are not in the current directory, then you have to specify the full path to the source files
set(SOURCE_FILES main.cpp func.cpp)
You can use file(GLOB SOURCE_FILES *.cpp) if you want to automatically add files in your compilation. But keep in mind that this "trick" is strongly not encouraged to do.

Compile GLFW code with g++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am just starting to get into C++ coding, specifically OpenGL (GLFW at this stage). I am using MinGW, and compiling my code using a batch file:
g++ -time -v -I lib/glfw-3.1.2/include -o "bin/OpenGL Test" src/Main.cpp
My test code is this:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if(!glfwInit()) {
return 1;
}
glfwTerminate();
return 0;
}
To me this seems like it should compile and run correctly, but it throws errors at the two GLFW functions, claiming that they are 'undefined references'.
What have I done wrong?
'undefined references' is a common error. You should compile your cpp first. Then link it with the library to form executable file.

Hash function SHA1 in C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I want to use SHA1 function from openssl library for hashing a string, I have downloaded the library and installed it in /usr/include, and here is my code:
#include <openssl/sha.h>
#include <string.h>
#include <stdio.h>
int main() {
unsigned char digest[SHA_DIGEST_LENGTH];
char string[] = "hello world";
SHA1((unsigned char*) &string, strlen(string), (unsigned char*) &digest);
}
It doesn't have any syntax error and it recognizes openssl/sha.h, but when I want to build the project in eclipse or build from the terminal, I get this error:
Hash.cpp:(.text+0x4a): undefined reference to `SHA1'
collect2: error: ld returned 1 exit status
Any help would be appreciated! :)
You didn't properly link openssl, if you're on Linux, you should link crypto.
From a terminal :
g++ -o hash hash.cpp -lcrypto
From eclipse, you should open project->Properties, go to C/C++ Build->Settings and add crypto in the Linker->Libraries folder.