GLEW based program does not compile - c++

I am trying to use GLEW in a program I'm creating, but my compiler will not compile it, instead it throws a ton of errors at this line gcc -g -c glew.c -o glew.o. This is my Makefile:
MY_LIBS =
glewex: glew.o main.o glew.h
g++ main.o glew.o glew.h -o glewex $(MY_LIBS)
glew.o: glew.c
gcc -g -c glew.c -o glew.o
main.o: main.cpp
g++ -g -c main.cpp -o main.o
It simply outputs hundreds of errors that look like this:
__glewActiveTexture redeclared without dllimport attribute: previous import ignored [ -Wattributes ]

Try this:
gcc -g -DGLEW_STATIC -c glew.c -o glew.o
That should prevent DLL import/export decorations from getting added to the declarations.

You don't want to add the library source files to the compiler input of your project. You should add the library to the list of linker inputs; either statically (libglew.a) or dynamic (-lglew).
I.e. either
gcc -o … -lglew
or
gcc -o … libglew.a
When linking GLEW statically you must add -DGLEW_STATIC to the compiler options generating the compilation units (.o files)

Related

g++ creates shared object with dynamic dependency even though I gave it a static archive in the inputs

I have a source file, mything.cpp, and a library that was provided to me as notmine.h and notmine.a.
I need to produce a shared object that has all my stuff from mything.cpp and all the stuff from somelib.a. Here is what I did on the command line:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o notmine.a
However, when I look at the final mything.so using ldd I see that it has a dependency on libnotmine.so, and when I check nm, I see that all the symbols that should have been supplied by notmine.a are undefined.
What am I doing wrong?
More details: notmine.a is actually liblua.a that I built locally. I think g++ might be getting confused because there is a liblua.so in the system directories
Finally figured it out. There are two options. The simpler is to use:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -L. -l:notmine.a
Alternatively, you can tell the linker you want to treat the .a as a bunch of object files with
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -Wl,--whole_archive notmine.a -Wl,--no-whole-archive
The --Wl,-no-whole-archive is to prevent that flag from messing up the other steps the linker does with the system libraries

problem with including fmt library in several cpp files

Good day!
I installed the fmt library in Ubuntu. Added it in my project
#include "fmt/core.h"
#include "fmt/format.h"
#include "fmt/format-inl.h"
to use fmt::format_int и fmt::format. I added library headers in several cpp files of my project. During linkage I got the mistake "multiple definition":
obj/container.o: In function fmt::v7::format_error::~format_error()': container.cpp:(.text+0x40e): multiple definition of fmt::v7::format_error::~format_error()'
obj/line.o:line.cpp:(.text+0x40e): first defined here
I've read something about this mistake. It is recommended to divide declaration and implementation in h and cpp files, to set some status to objects that causes mistake and so on. But all this recommendations imply editing of library (not my!) code!
What is wrong?
I do the following
compilation of files - one by one
g++ -std=c++11 -Wall -o obj/line.o -c /home/...//line.cpp
g++ -std=c++11 -Wall -o obj/container.o -c /home/...//container.cpp
g++ -std=c++11 -Wall -o obj/geometryObject.o -c /...//geometryObject.cpp
g++ -std=c++11 -Wall -o obj/model.o -c /home/...//model.cpp
g++ -std=c++11 -Wall -o obj/point.o -c /home/...//point.cpp
g++ -std=c++11 -Wall -o obj/main.o -c /home/...//main.cpp
Linking - error here
g++ -std=c++11 -Wall -o myapp obj/line.o obj/container.o obj/geometryObject.o obj/model.o obj/point.o obj/main.o
You shouldn't be including fmt/format-inl.h because it's an internal header. Please see the documentation for the list of public headers and what they provide.

Undefined symbols for architecture x86_64 when linking multiple files

I'm trying to link three files using g++. The files are simulation.o, lattice.o and thermodynamics.o.
They're a bit long, but the gist of it is. I have a makefile:
main: simulation.o thermodynamics.o lattice.o
g++ simulation.o thermodynamics.o lattice.o
simulation.o: simulation.cpp lattice.o lattice.h thermodynamics.o thermodynamics.h
g++ -std=c++11 simulation.cpp -o simulation.o -c
thermodynamics.o: thermodynamics.cpp
g++ -std=c++11 thermodynamics.cpp -o thermodynamics.o -lgsl -c
lattice.o: lattice.cpp
g++ -std=c++11 lattice.cpp -o lattice.o -c
It passes the compile stage, but never links them. For each method I need from a different file, it simply says that it's undefined, and and refuses to find them.
The classes and methods are all defined in the .h files. But for some reason I can define an external function but not an external class.
It fails to link because your makefile uses linker flags when compiling. Whereas the linker flags must be used when linking.
Corrections:
CXXFLAGS := -std=c++11 -Wall -Wextra
main: simulation.o thermodynamics.o lattice.o
g++ -o main simulation.o thermodynamics.o lattice.o -lgsl
simulation.o: simulation.cpp lattice.h thermodynamics.h
g++ -c ${CXXFLAGS} -o simulation.o simulation.cpp
thermodynamics.o: thermodynamics.cpp thermodynamics.h
g++ -c ${CXXFLAGS} -o thermodynamics.o thermodynamics.cpp
lattice.o: lattice.cpp lattice.h
g++ -c ${CXXFLAGS} -o lattice.o lattice.cpp
I figured out what went wrong. Had to do with the way I've split the classes between files.
I declared the classes in the .h files, and then redeclared them inside .cpp files. Instead I should have filled the cpp files with implementations of the form class::method(params).
Also I didn't #include the .h file inside the .cppfile.
Lastly, I also had the wrong order of linkage: as pointed out by #Maxim Egorushkin, the order of linking matters. I should have linked the files all at once, and not at the final stage.
Thanks for everyone that answered!

Link To Library with C and C++ Bindings

I am trying to determine if it is possible to link against a linux library with c and c++ bindings. I have an existing c++ project which is broken down into several libraries. I would like to extend these libraries with C bindings so that I can use them with cgo.
I am aware of how to mix c/c++ with extern "c" syntax. The problem that I have is in daisy chaining libraries. I've created a sample project which demonstrates my question:
https://github.com/Shelnutt2/c_cpp_linker_test
In this project we have hello.c, world.cpp both of which are built into a library called libwords . I can link libwords to libhelloworld without an issue. The problem occurs when trying to build main.cpp, which wants to call a c and c++ functions from libhelloworld (and thus the linked libwords)
Due to the linking differences of C++ vs C the main executable can not find the hello function
main.cpp:15: undefined reference to `hello'
Is it possible to link against the same library in this manner or do I need to break c-bindings into their own wrapper library?
In this example project I used shared libraries, but I'm open to static linking if that is possible.
You should link your application with both libraries. The exports are not transitive.
main: main.o libhelloworld.so
$(CXX) $< -L'$(CURDIR)' -lwords -lhelloworld -o $#
> gmake
c++ -Wall -g -O -fPIC -DC_LINK_TEST=1 -c -o helloworld.o helloworld.cpp
cc -Wall -g -O -fPIC -DC_LINK_TEST=1 -c -o hello.o hello.c
c++ -Wall -g -O -fPIC -DC_LINK_TEST=1 -c -o world.o world.cpp
c++ -Wall -g -O -fPIC -DC_LINK_TEST=1 -shared hello.o world.o -o libwords.so
c++ -Wall -g -O -fPIC -DC_LINK_TEST=1 -L'/usr/home/me/c_cpp_linker_test' -lwords -shared helloworld.o libwords.so -o libhelloworld.so
c++ -Wall -g -O -fPIC -DC_LINK_TEST=1 -c -o main.o main.cpp
#c++ main.o libhelloworld.so -o main
c++ main.o -L'/usr/home/me/c_cpp_linker_test' -lwords -lhelloworld -o main
> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd` ./main
c gives us: hello
cpp gives us: hello world
>
Otherwise there is no problem with linking both C and C++ calling conventions, as the name mangling is different.
(never mind the change g++ to c++ - I use clang)

Find library corresponding to a file

I am working on a c++ project. It's output are so files. produced by calling a make file.
For my test cases(using cppunit) I want a output in the form of an executable.
I tried the following command g++ -o output File1.o -lcppunit. It gives me the output file with proper results.
But if I include some file from the project(File2) in the test case program (whose .o file I have), it gives me various unresolved symbols. I get that error if in the previous command I hadn't put the -lcppunit. So I understand, that corresponding to the symbol I need it's library. But I dont know what library to include.
Please help me with that. Any help would be appreciated.
These are various commands that are run from the makefile.
To get the .o file:
g++ -Wall -g -fPIC -D_REENTRANT -DRW_MULTI_THREAD -DRW_NO_STL -DRW_COMPILE_INSTANTIATE -DRW_BCB_NEW_IOSTREAMS -DRW_RWV2X_STDLIB -DRW_NO_XDR -D_GLIBCPP_DEPRECATED -DPM_32BIT -DFD_SETSIZE=4096 -DUNIX -DLINUX -DCHECK_BUFPOOL_INTEGRITY -DOPB_DEBUG -DRWDEBUG -DDEBUG -DINFA_PREFIX -I. -IAllIncludePaths -c File.cpp -o File.o
To get the so files:
g++ -shared -ldl -lpthread -Wl,--hash-style=both File.o File1.o File2.o File3.o
-o /xyz.so
-LAlllinkPaths -llibraries
also what should my all: in makefile have?