How to build from multiple source files at once in Eclipse - c++

I have an Eclipse C++ project which initially has first.cpp. Then second.cpp is added and should be linked to the original file. Using Eclipse building tool, I got this output:
make all
Building file: ../src/first.cpp
Invoking: GCC C++ Compiler
g++ -I/home/workspace/first/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/first.d" -MT"src/first.o" -o "src/first.o" "../src/first.cpp"
Finished building: ../src/first.cpp
Building file: ../src/second.cpp
Invoking: GCC C++ Compiler
g++ -I/home/workspace/first/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/second.d" -MT"src/second.o" -o "src/second.o" "../src/second.cpp"
Finished building: ../src/second.cpp
Building target: first
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "first" ./src/first.o ./src/second.o
Finished building target: first
How can I get Eclipse to compile this way?
g++ first.cpp second.cpp -o first
Thank you so much.
============================================================================
I am asking how to make a single binary from multiple source files, not building multiple binaries with multiple source files.

Try using CMake
As per my understanding of your question, you would need to add your source files into CMakeList.txt and then run it. You can make use of this tutorial in doing so.

Related

Override -shared option in g++

I was building some Cython extensions, and have to link it against a static library (it has CUDA code in them, so have to be static):
running build_ext
building 'k3lib' extension
gcc -pthread -B /home/kelvin/anaconda3/envs/torch/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/kelvin/repos/tools/include -I/home/kelvin/anaconda3/envs/torch/include/python3.8 -c main.cpp -o build/temp.linux-x86_64-3.8/main.o -O3 -march=native
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
g++ -pthread -shared -B /home/kelvin/anaconda3/envs/torch/compiler_compat -L/home/kelvin/anaconda3/envs/torch/lib -Wl,-rpath=/home/kelvin/anaconda3/envs/torch/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/main.o /home/kelvin/repos/tools/include/libk2.a -L/home/kelvin/repos/tools/include -lk2 -o build/lib.linux-x86_64-3.8/k3lib.cpython-38-x86_64-linux-gnu.so -static -Wl,-Bstatic -flinker-output=exec
However, Cython's g++ compile command includes the options -shared -fPIC by default. I tried a number of options at the end of the command via this setup file (the static library is at $(LOCAL_INCLUDE)/libk2.a):
includes = [os.getenv("LOCAL_INCLUDE")]
ext_modules = [
Extension("k3lib", sources=["main.pyx"],
libraries=["k2"], include_dirs=includes, library_dirs=includes, language="c++",
extra_compile_args=["-O3", "-march=native"], extra_objects=[f"{includes[0]}/libk2.a"],
extra_link_args=['-static', '-Wl,-Bstatic', '-flinker-output=exec'])
]
#extra_objects=[f"{includes[0]}/libk2.a"]
#extra_link_args=['-static']
setup(name="k3lib", ext_modules=cythonize(ext_modules, language_level="3"))
Still, g++ thinks that I want to build a shared library, and thus the error message. Is there a way to override the -shared option? I'm planning to go into Cython's files and edit them myself, but was wondering is there a simpler way?
Context: I was following this question on SO but can't replicate their success.

How to compile an independent executable file by g++?

I devellop a piece of c++ software on my OpenSUSE, which I like to test on another OpenSUSE system.
I copied the executable file on the other system, but when I am starting it I receive an error:
"error while loading shared libraries: libboost_system.so.1.61.0:
cannot open shared object file: No such file or directory"
How can I compile an independent executable without dependencies?
Eclipse does this:
17:24:41 **** Incremental Build of configuration Debug for project boostServer ****
make all
Building file: ../src/boostCom.cpp
Invoking: Cross G++ Compiler
g++ -I/opt/boost -I/usr/local/lib -O0 -g3 -Wall -c -fmessage-length=0 -isystem /opt/boost -MMD -MP -MF"src/boostCom.d" -MT"src/boostCom.o" -o "src/boostCom.o" "../src/boostCom.cpp"
Finished building: ../src/boostCom.cpp
Building file: ../src/main.cpp
Invoking: Cross G++ Compiler
g++ -I/opt/boost -I/usr/local/lib -O0 -g3 -Wall -c -fmessage-length=0 -isystem /opt/boost -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp
Building target: boostServer
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "boostServer" ./src/boostCom.o ./src/boostServer.o ./src/main.o -lboost_system -lboost_serialization -lboost_thread -lboost_date_time -lpthread
Finished building target: boostServer
Greets Ulf
You need to make sure you have the boost libraries installed on your system, and that the path to them is included in you LD_LIBRARY_PATH variable.
For example, if the libboost_system.so file (or symlink) is to be found at /usr/local/boost-1.56.0/lib64 on your system, then you would run the command.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/boost-1.56.0/lib64`
To find out exactly which libraries you need, you can run the command:
ldd my_binary
The output will tell you exactly which libraries are required, and which of those can't currently be resolved.

Mosek C-API using MinGW

I try to build the 'lo1' Example for Mosek on MinGW. Unfortunately, it keeps returning undefined reference to ... I think I'm doing something wrong when linking the libraries. Anyone can help?
My build commands are the following:
g++ "-IC:\Program Files\Mosek\7\tools\platform\win64x86\h" "-LC:\Program Files\Mosek\7\tools\platform\win64x86\bin" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "lo1.o" "lo1.cpp" "-lmosek64_7_1"
g++ "-LC:\Program Files\Mosek\7\tools\platform\win64x86\bin" -static-libgcc -static-libstdc++ -o lo1.exe "lo1.o" "-lmosek64_7_1"
Is the problem happening while building or while running the final binary?
Are you sure g++ builds a 64bit binary. Maybe you should add a -m64
g++ "-IC:\Program Files (x86)\Mosek\7\tools\platform\win32x86\h" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "lo1.o" "lo1.cpp"
g++ "-LC:\Program Files (x86)\Mosek\7\tools\platform\win32x86\bin" -static-libgcc -static-libstdc++ -o lo1.exe "lo1.o" "-lmosek7_1"
Thanks for your answer. the problem happened while building.
The 64bit version did not work. Mosek 32bit works with MinGW. Above build commands were used.

C++ compilation error (eclipse)

I am working on simple program of grayscaling using Opencl. When i compile, it gives me the following error.
make all
Building file: ../Test1.cpp
Invoking: GCC C++ Compiler 4.9.3 [armeb-linux-gnueabihf]
armeb-linux-gnueabihf-g++ -I/opt/AMDAPPSDK-3.0-0-Beta/include/ -I/opt/AMDAPPSDK-3.0-0-Beta/include/CL -I/opt/AMDAPPSDK-3.0-0-Beta/include/GL -I/opt/AMDAPPSDK-3.0-0-Beta/include/SDKUtil -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -o -MM -MMD -MP -MF"Test1.d" -MT"Test1.d" -o "Test1.o" "../Test1.cpp"
cc1plus: fatal error: Test1.d: No such file or directory
compilation terminated.
make: *** [Test1.o] Error 1
Compiler command with options:-
armeb-linux-gnueabihf-g++ -I/opt/AMDAPPSDK-3.0-0-Beta/include/ -I/opt/AMDAPPSDK-3.0-0-Beta/include/CL -I/opt/AMDAPPSDK-3.0-0-Beta/include/GL -I/opt/AMDAPPSDK-3.0-0-Beta/include/SDKUtil -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -o -MM
The actual command that is executed seems to have the output option -o twice. This option is supposed to be followed by a filename.
Please remove the output option -o from your command because eclipse seems to automatically add it.

mongoDB, c++ libmongoclient.so: cannot open shared object file: No such file or directory

when I build my cpp project in cpp...this is the oupput.
**** Build of configuration Debug for project rtbCookieServer ****
make all
Building file: ../src/rtbCookieServer.cpp
Invoking: GCC C++ Compiler
g++ -I/home/cpp/mongo-cxx-driver-v2.0/mongo -I/home/cpp/mongo-cxx-driver-v2.0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/rtbCookieServer.d" -MT"src/rtbCookieServer.d" -o"src/rtbCookieServer.o" "../src/rtbCookieServer.cpp"
Finished building: ../src/rtbCookieServer.cpp
Building target: rtbCookieServer
Invoking: GCC C++ Linker
g++ -L/home/cpp/mongo-cxx-driver-v2.0 -lfcgi++ -lboost_system -lcgicc -lmongoclient -o"rtbCookieServer" ./src/rtbCookieServer.o
Finished building target: rtbCookieServer
W=hen I run the code..the is the error message I get.
/home/workspace/rtbCookieServer/Debug/rtbCookieServer: error while loading shared libraries: libmongoclient.so: cannot open shared object file: No such file or directory
The file is in home/cpp/mongo-cxx-driver-v2.0 so why cant if find it????
Thanks
Better than using LD_LIBRARY_PATH is to specify the runtime library search
-Wl,-rpath /home/cpp/mongo-cxx-driver-v2.0
For more information about why not using LD_LIBRARY_PATH look e.g. here.
Try the command
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/cpp/mongo-cxx-driver-v2.0
first, and try again.
The loader doesn't know the path to the library, so it has to be told where to look.