I've written a simple code which create folder. The problem is that I can't compile it. The code is below:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directories("/tmp");
return 0;
}
Compilation:
g++ createFolder.cpp -std=c++0x -lboost_system -o createFolder
I have got errors:
collect2: ld returned 1 exit status
How to correct the compilation process to run this program.
Try adding boost-filesystem to you linker:
g++ createFolder.cpp -std=c++0x -lboost_system -lboost_filesystem -o createFolder
Related
I try to compile JDK source code on Ubuntu 20.04. However, the configure script of JDK failed with some errors:
// conftest.cpp
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
FT_Error error = FT_Init_FreeType(&library);
if (error) {
printf("Error occurred during init.\n");
}
else {
printf("Initialised OK!\n");
}
return 0;
}
In Terminal, this can link OK:
$ g++ -o conftest -I/usr/include/freetype2 -L/usr/lib/x86_64-linux-gnu conftest.cpp -lfreetype
but this not work:
$ g++ -o conftest -I/usr/include/freetype2 -L/usr/lib/x86_64-linux-gnu -lfreetype conftest.cpp
/usr/bin/ld: /tmp/ccpFC5Ih.o: in function `main':
conftest.cpp:(.text+0x23): undefined reference to `FT_Init_FreeType'
collect2: error: ld returned 1 exit status
Only difference is the order of the parameters and,
from this freetype question1, it seems that it's a upgrade problem. I am pretty sure that already the current latest 20.04,
$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0)
I am trying to compile this piece of code using GCC:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main(){
return 0;
}
I am following this guide. As the guide said I went to https://glad.dav1d.de/ and generated glad. I placed the glad and KHR include folders in my /usr/include folder and placed glad.c in the folder where I have this piece of code.
This is the command I am trying to use to compile the code:
gcc test.cpp -o test -lglad -lglfw
I get this compile error
/usr/bin/ld: cannot find -lglad
collect2: error: ld returned 1 exit status
Turns out, you can just compile using
gcc test.cpp glad.c -o test -lglfw
I'm trying to make a C++ script that will run some simple Python code:
// t.cpp
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Upon running g++ t.cpp, I get the error:
t.cpp:1:20: fatal error: Python.h: No such file or directory
compilation terminated
I've found many similar questions, all specific to an IDE or other development software, or were solved by installing python3-dev. The python3-dev package is already installed, and I even tried manually including the header when attempting to compile:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
Neither changes anything.
How can I fix this error?
UPDATE: I found that using g++ t.cpp -I /usr/include/python3.5/ seems to include the header, but then it runs into more errors:
t.cpp:(.text+0x10): undefined reference to `Py_Initialize'
t.cpp:(.text+0x1f): undefined reference to `PyRun_SimpleStringFlags'
t.cpp:(.text+0x24): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
I've set up a similar example on my github
g++ t.cpp is missing a few things:
Tell g++ where the headers are for cpython (by -I/path/to/headers/)
Tell g++ to link against libpython (by -lpython3.5m)
You can also retrieve these flags with pkg-config
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m
Your commandline should look something like g++ -I/usr/include/python3.5m t.cpp -lpython3.5m
#include <...> is for includes that come with the compiler.
Use #include "Python.h" for any other includes.
Run the following commands to compile your code:
mytest.cpp:
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Compile:
$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest
I am trying to compile my program on my new server, but it's not working for me at the moment.
Error log is:
rasmus#web01:~/c++$ make test
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
/tmp/ccPaMZUy.o: In function `CPULogger':
/home/rasmus/c++/cpulogger.cpp:7: undefined reference to `mysql_init'
/home/rasmus/c++/cpulogger.cpp:8: undefined reference to `mysql_real_connect'
/home/rasmus/c++/cpulogger.cpp:10: undefined reference to `mysql_get_client_info'
/tmp/ccPaMZUy.o: In function `~CPULogger':
/home/rasmus/c++/cpulogger.cpp:16: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make: *** [all] Error 1
As you can see I am compiling against MySQL - I have checked that mysql.h is present in include paths.
What am I missing?
cpulogger.cpp has #include "cpulogger.h" at the top, then cpulogger.h has this:
#include <iostream>
#include <fstream>
#include <mysql/mysql.h>
The compiler does not complain about missing mysql/mysql.h, so that part must work?
Output of mysql_config:
rasmus#web01:~/c++$ mysql_config --cflags --libs
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
-L/usr/lib -lmysqlclient -lpthread -lz -lm -lrt -ldl
Makefile:
all:
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
test: all
./test
It's a fresh Ubuntu server installation with a mysql-server install on it.
[solved]:
Putting linker libraries at the end of the compiler commands works.
all:
g++ main.cpp logger.cpp cpulogger.cpp -o test `mysql_config --cflags --libs`
See answer below for explanation.
The order of arguments to the linker is significant. Use mysql-config after listing the files that need it. The linker will see that cpulogger.o needs mysql_init and look in libraries listed after it for the symbol. If the libraries were listed earlier in the arguments they won't be searched again.
Why this does not work, file test.c:
#include <event.h>
int main(void)
{
event_init();
return 0;
}
Then:
gcc -o test.o -c test.c runs OK, but
Link:
g++ -o test -levent test.o produces
test.o: In function `main':
test.c:(.text+0x5): undefined reference to `event_init'
collect2: ld returned 1 exit status
So it cannot be linked as C++. How to solve this? I need to link it as C++ and compile as C.
This question has been asked many times. On Linux, you should put libraries after object and source files in the compilation command. So try
g++ -Wall -g -c mytest.cc
g++ -Wall -g mytest.o -levent -o mytest
Avoid calling your test program test which is an existing utility or shell builtin.
As a newbie, remember to always compile with all warnings asked -Wall and for debugging -g and learn to use gdb