I'm trying to compile a simple program in c++ with gsl. On our university server we have installed GSL. The main problem is, that I'm compilling:
g++ atest.cpp -c -lgsl -lgslcblas -c lm
And after that, I'm typing:
./a.out
And I get :
-bash: ./a.out : No such file or directory
What's the problem? Thanks.
You're only compiling, not linking. From man gcc:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
By default, the object file name for a source file is made by
replacing the suffix .c, .i, .s, etc., with .o.
Unrecognized input files, not requiring compilation or assembly,
are ignored.
So -c option (command line argument) means compile
g++ atest.cpp -c -lgsl -lgslcblas -lm
g++ atest.o
this will produce a.out file, or you can use short hand
g++ atest.cpp -lgsl -lgslcblas -lm
which will compile and link code in one.
Plus, you could use ls to check your directory does it contain a.out before trying to run it.
Related
I want to use OpenGL GLEW library. I have the binary downloaded and its folder is in the folder with my .cpp file. My .cpp file uses #include <eglew.h>.
How should I format my command for MinGW to compile my .cpp file? Do I compile with the .lib file like g++ -L./path/to/lib/file.lib test.cpp -o test or do I do something else like link to the header files g++ -I./path/to/headers test.cpp -o test?
To better understand things maybe it's better to split compiling and linking steps.
If you get errors then you will also know in which step the problem occurs.
I'm assuming you have the following folders/files:
/path/to/eglew/include/GL/eglew.h
/path/to/eglew/lib/libglew32.a
Compiling:
g++ -Wall -c -o test.o test.cpp -I/path/to/eglew/include/GL
Linking:
g++ -o test.exe test.o -L/path/to/eglew/lib -lglew32
Though I would expect to see #include <GL/eglew.h> in which case the linker include flag should be -I/path/to/eglew/include.
There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.
For example, I'm given carModels.cpp, carModels.h, carType.in, manufacturers.h, manufacturers.o, and lastly my own file tester.cpp. How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)
Compile each source file to its own object file:
g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o
Now link all object files together:
g++ carModels.o tester.o manufacturers.o -o outputname
Consider adding more options like -O3, -std=c++11, -Wall, etc. as needed.
you can do this in two steps, first compile to *.o files,
gcc -c your.cpp other.cpp .....
then link them
gcc -o you_out_put_name the_object_files.o ...
In a single line, that would be just g++ -o tester *.cpp *.o. GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.
I've search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.
I'm making a makefile for a school project and as part of my project I'm generating a static library and linking against it. The compiler throws an error when it gets to a header include in the static library. The code for that is just #include "StringUtil.h"
So in the makefile I have these relevant parts of code
LINKFLAGS=-Llib/ -lHTMLtools
bin : lib $(BIN_FILE)
lib : $(LIB_OBJ_FILES)
ar r lib/libHTMLtools.a $(LIB_OBJ_FILES)
$(BIN_FILE) : $(OBJ_FILES) #This is only obj/crawler.o for now
g++ -o bin/crawler obj/crawler.o
obj/crawler.o : src/crawler.cpp inc/crawler.h
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
so whenever I run the make bin command it generates lib.libHTMLtools.a as expected but when it gets to the
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
line it returns this error.
src/crawler.cpp:2:24: fatal error: StringUtil.h: No such file or directory compilation terminated.
Any help or advice will be appreciated!
In C++, library files are not enough. They are not used when compiling source code, but only when linking. To compile source file, you need to include headers. But the compiler need to know where to find it. Try adding -I utils/inc to your last line like this
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc -I utils/inc src/crawler.cpp
I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.
I have source file in test.cpp.
To compile this, I did
(1)
g++ -c test.cpp
g++ -o test test.o
./test
Everything works fine.
But when I did compling and linking in same stage, like this
(2)
g++ test.cpp -o test
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work
In my last case, test is generated but is no more executable; but in my guess it should work fine.
So, what is wrong or do I need to change some settings/configuration ??
I am using g++ 4.3.3
Thanks.
When you say:
g++ -c test.cpp -o test
The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.
Basically, don't do that.
You are forcing compiler to produce an object file and name it like an executable.
Essentially your last line tells: compile this to an object file, but name it test, instead of test.obj.
-c flag means Compile Only
Try
g++ -o test test.cpp
Specifying -o in the g++ command line tells the compiler what name to give the output file. When you tried to do it all in one line, you just told the compiler to compile test.cpp as an object file named test, and no linking was done.
Have a look at the fabulous online manual for GCC for more details.
from the gcc manual:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
You must link the compiled object files to get the executable file.
More info about compiling and linking and stuff is here.
Read man g++. The switch -c is to compile only but not to link.
g++ -c test.cpp -o test
does what
g++ -c test.cpp
does but the object file will be test istead of the default name test.o. An object file cannot be executed.