Link .so file to .cpp file via g++ compiling
now from what i can see the command is
-l + filename
, for example my filename isdirectory/libtest.so
it would be-ldirectory/libtest.so
No, that's not correct. It should be -Ldirectory -ltest
i.e. you use -L
to add a directory to the search paths where the linker will look for libraries, and you say which libraries to link to with -l
, but to link to libtest.so
or libtest.a
you say -ltest
without the lib
prefix or the file extension.
You can link by naming the file explicitly, without -L
or -l
options, i.e. just directory/libtest.so
, but for dynamic libraries that is almost always the wrong thing to do, as it embeds that exact path into the executable, so the same library must be in the same place when the program runs. You typically want to link to it by name (not path) so that the library with that name can be used from any location at run-time.
Linking files in g++
You probably tried to either compile and link instead of just compiling source files or somehow forgot something.
Variation one (everything in one line; recompiles everything all the time):
g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]
Variation two (step by step; if no -o
is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):
g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]
Variation three (some placeholders):
Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp
will compile all cpp files in current directory to o(bject) files of the same name.
Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.
Build a .so file from multiple C++ files and use it
A makefile like follow:
all:a.so
a.so: outer.o b.o c.o
g++ $^ -fPIC -shared -o $@
%.o:%.cc
g++ $< -I(your header file path) -c -o $@
Don't forget the -c flag when generating .o files
if the liba.so in same folder with main.cc you can
g++ main.cc -la -o main
the main.cc should include outter.h b.h c.h
or you rename a.so to liba.so then put it into /usr/lib/
and run g++ main.cc -la -o main
Can I instruct g++ to compile a c++ source file with a different extension than .cpp etc?
You can explicitly specify the language of a source file using the -x <lang>
option:
g++ -x c++ file.strange_extension
Compiling a .cpp file with c++ command instead of g++ (Linux)
Shouldn't it throw an error saying there is no c++ command available? and suggest us to use g++?
Weeelll, there are no regulations or standards that restrict or require c++
command to do anything, so there is no "should" or "shouldn't". However, it would be strongly expected that c++
is a working C++ compatible compiler, that supports similar or same flags as cc
does.
does the terminal replace our c++ hello.cpp with g++ hello.cpp internally?
A terminal is the device that displays things. Terminal does not replace it, it has no effect on it.
Most probably your system designer, but maybe administrator or publisher or distributor or package designer (or anyone in the chain), configured your system to provide a command named c++
. Usually, that command is a symbolic link to a working C++ compiler, usually g++ on Linux systems. On my system, /usr/bin/c++
program is just installed with package named gcc
, but some systems allow it to be configurable.
It shouldn't do that right?
As stated above, terminal shouldn't replace commands, it has no effect on it.
This is expected since
It is expected since there is no command named c
. There are endless other unknown commands.
cc
is the good old name for a C compiler. c99
is the standardized name of C99 compatible compiler.
Why am not getting a similar error for the c++ hello.cpp?
Because a command named c++
exists on your system.
Related Topics
How to Generate Random Numbers in C++
How to Securely Disconnect an Asio Ssl Socket
Catching "Stack Overflow" Exceptions in Recursive C++ Functions
What Is the Use of 0-Length Array (Or Std::Array)
Stopping the Debugger When a Nan Floating Point Number Is Produced
What Does "#Define Str(A) #A" Do
Why Can't I Do Polymorphism with Normal Variables
How to Run Specific Test Cases in Googletest
Should I Inherit from Std::Exception
Why Does Wide File-Stream in C++ Narrow Written Data by Default
What Is the Meaning of Auto When Using C++ Trailing Return Type
How to Read Bmp Pixel Values into an Array
Cmake Cannot Determine Linker Language for Target
Is Infinite Loop Still Undefined Behavior in C++ If It Calls Shared Library
What Is the Vtable Layout and Vtable Pointer Location in C++ Objects in Gcc 3.X and 4.X