How do you compile ImGui on Windows vscode MinGW? - c++

Right, so I have been trying for weeks now to get ImGui to work with my windows OpenGL project. I have tried everything. So, can someone please show me how to compile ImGui with the rest of my project. Like how to add it to the vs code task file or how to add it to my makefile. Also when I have asked before people have said use CMake. But I can't because CMake doesn't work so I can't use it and I don't want to waste more time to try and get it to work. So, anyone who knows how to help please do.
Here is my makefile:
CXX = g++
Flags = -std=c++17 -Wall -Wformat
Libs = -lGL -lGLEW -lglfw -ldl
OBJDIR = obj
VSXFLAGS = src/vendor/imgui/imgui_impl_glfw.h src/vendor/imgui/imgui_impl_opengl3.h
Filez = src/Rendering/*.cpp src/Rendering/*.h src/Core/*.h src/Core/*.cpp src/vendor/glm/gtc/type_ptr.hpp
Includes = -Isrc/PCH/ -Isrc/ -Iinclude/ -Isrc/Rendering/Buffers/ -Isrc/Core/ -Isrc/Rendering/ -Isrc/vendor/imgui -Isrc/FileManagement/ -Ires/ -Isrc/vendor/glm/ -Isrc/vendor/stb_image -Isrc/vendor/
all: main.cpp
$(CXX) $(Flags) $(Includes) $(Filez) main.cpp lib/imgui.a lib/Buffers.a -o main $(Libs)
clean:
rm $(OBJDIR)/*.o *.o
This is my file structure:
[]2

As ImGui is a separate library your project should link against it rather than compiling it with the rest of your project each time.
Here's how I build the library (both static and shared) in MSYS2 shell:
# change the next value to where you want to install it
export INSTALLPREFIX=/D/Prog/3rdparty
# fix DLL exports in imgui.h
mv imgui.h imgui.h.bak
cat > imgui.h << EOF
#if !defined(IMGUI_API) && defined(_WIN32)
#if defined(BUILD_IMGUI_SHARED)
#define IMGUI_API __declspec(dllexport)
#elif !defined(STATIC) && !defined(STATIC_IMGUI)
#define IMGUI_API __declspec(dllimport)
#else
#define IMGUI_API
#endif
#endif
EOF
sed -e "s/static char\s*EmptyString/IMGUI_API &/" imgui.h.bak >> imgui.h
# don't export inline functions in imgui_internal.h (version >= 1.81)
sed -i.bak -e "s/IMGUI_API \(~*ImGuiTable()\)/\1/" imgui_internal.h
~/buildstatus.sh build &&
touch SUCCESS &&
for F in *.cpp; do
echo CXX $F &&
g++ -O3 -I. $F -c -o $F.o -DBUILD_IMGUI_SHARED || rm -f SUCCESS
done &&
ls -1 SUCCESS > /dev/null &&
ar cru libimgui.a *.cpp.o &&
( echo EXPORTS; nm -f posix --defined-only -p libimgui.a | sed -n -e "s/^\([^ ]*\) T .*$/\1/p" | sed -e "s/^__*/_/" | grep "Im" ) > imgui.def &&
g++ -shared -s -mwindows -def imgui.def -o imgui.dll libimgui.a -Wl,--out-implib,libimgui.dll.a -Wl,--as-needed -limm32 &&
~/buildstatus.sh install &&
mkdir -p $INSTALLPREFIX/include/imgui $INSTALLPREFIX/lib $INSTALLPREFIX/bin &&
cp -f *.cpp *.h $INSTALLPREFIX/include/imgui/ &&
cp -f *.a $INSTALLPREFIX/lib/ &&
cp -f *.dll $INSTALLPREFIX/bin/ &&
echo SUCCESS
Make sure your MSYS2 environment points to the same compiler as VSCode in case its different (add to PATH).
My build was done with a recent standalone GCC/MinGW-w64 from https://winlibs.com/

Related

llvm-link: error : expected top-level entity

I'm trying to compile the libpng 1.2.56 file and use a target.cc(same as fuzzer-test-suite to unite all these file in libpng1.2.56 into one LLVM IR bitcode file, named combined.bc.
Here is my compile process, refering build.sh:
[ ! -e libpng-1.2.56.tar.gz ] && wget https://downloads.sourceforge.net/project/libpng/libpng12/older-releases/1.2.56/libpng-1.2.56.tar.gz
[ ! -e libpng-1.2.56 ] && tar xf libpng-1.2.56.tar.gz
build_lib() {
rm -rf BUILD
cp -rf libpng-1.2.56 BUILD
(cd BUILD && ./configure --disable-shared && make -j $JOBS )
}
build_lib || exit 1
mkdir bitcode
clang++-10 -g -flto -std=c++11 ./target.cc -c -o bitcode/target.o -I .
cd bitcode
ar x ../.libs/libpng12.a
llvm-link-10 *.o -o combined.bc
And error is
enter image description here
The bitcode folder:
enter image description here
For modifications like the above, I succeeded on the project json, but not on libpng.
I am a beginner in LLVM and would like to get answers from you all. Thanks a lot!

Trouble building GLEW on Windows using MinGW

I'm trying to build GLEW 2.1.0 on Windows (8.1) using MinGW and have been struggling for a while now.
I tried to follow the instructions for "MSYS/Mingw" on the GLEW GitHub page but when running mingw32-make, I only got a couple errors about missing commands (test and config) as well as the following: Makefile:40: *** "Platform '' not supported". Stop.
Under "Requirements" it says that bash is needed, so I installed git, because that apparently comes with a version of bash. Running mingw32-make (in the directory that all the GLEW files and folders are in) using Git Bash results in a long list of warnings and "undefined reference"s. The latter come from the linker as far as I know, but I have no idea what could cause them. As it's so much text, I'll only include the last few lines here:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: tmp/mingw/default/shared/glewinfo.o:glewinfo.c:(.text.startup+0x181b): undefined reference to `_imp___iob'
collect2.exe: error: ld returned 1 exit status
Makefile:181: recipe for target 'bin/glewinfo.exe' failed
mingw32-make: *** [bin/glewinfo.exe] Error 1
mingw32-make install has the following output:
$ mingw32-make install
install -d -m 0755 "/usr/include/GL"
install -m 0644 include/GL/wglew.h "/usr/include/GL/"
install -m 0644 include/GL/glew.h "/usr/include/GL/"
install -m 0644 include/GL/glxew.h "/usr/include/GL/"
sed \
-e "s|#prefix#|/usr|g" \
-e "s|#libdir#|/usr/lib|g" \
-e "s|#exec_prefix#|/usr/bin|g" \
-e "s|#includedir#|/usr/include/GL|g" \
-e "s|#version#|2.1.0|g" \
-e "s|#cflags#||g" \
-e "s|#libname#|glew32|g" \
-e "s|#requireslib#|glu|g" \
< glew.pc.in > glew.pc
install -d -m 0755 "/usr/lib"
install -d -m 0755 "/usr/bin"
install -m 0755 lib/glew32.dll "/usr/bin/"
install -m 0644 lib/libglew32.dll.a "/usr/lib/"
install -m 0644 lib/libglew32.a "/usr/lib/"
install -d -m 0755 "/usr/lib/pkgconfig"
install -d -m 0755 "/usr/lib/pkgconfig"
install -m 0644 glew.pc "/usr/lib/pkgconfig/"
And mingw32-make install.all produces another long list of undefined references and the same error that came from running mingw32-make. The first lines however are (about) the same as the result of running mingw32-make install.
As expected with all of the errors, running these commands didn't produce a binary file. I guess it could have something to do with me not having make, which is one of the requirements listed on the GitHub page. However I don't understand how I should install make on Windows and why it is even needed when the instructions say to use mingw32-make.
I've also tried some of the suggestions made in this thread, but that doesn't seem to have worked either.
What could be the problem and what can I do to successfully compile GLEW?
I could never build it with the instructions I found around the internet, so here's how I build glew (using https://downloads.sourceforge.net/project/glew/glew/2.2.0/glew-2.2.0.tgz) under MSYS2 with MinGW-w64:
# change the line below to your desired install path
INSTALLPREFIX=/usr/local
VERSION=2.2.0
gcc -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32.a src/glew.o &&
gcc -DGLEW_MX -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32mx.a src/glew.mx.o &&
gcc -O2 -c -Iinclude -o src/glewinfo.o src/glewinfo.c &&
gcc -s -o src/glewinfo.exe src/glewinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 &&
gcc -O2 -c -Iinclude -o src/visualinfo.o src/visualinfo.c &&
gcc -s -o src/visualinfo.exe src/visualinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 -lglu32 &&
sed -e "s?#prefix#?$INSTALLPREFIX?; s?#libdir#?\$\{prefix\}/lib?; s?#version#?$VERSION?; s?#requireslib#?glu?; s?#cflags#??; s?#libname#?glew32?" glew.pc.in > glew.pc &&
sed -e "s?#prefix#?$INSTALLPREFIX?; s?#libdir#?\$\{prefix\}/lib?; s?#version#?$VERSION?; s?#requireslib#?glu?; s?#cflags#?-DGLEW_MX?; s?#libname#?glew32mx?" glew.pc.in > glewmx.pc &&
echo Installing... &&
mkdir -p $INSTALLPREFIX/include $INSTALLPREFIX/lib/pkgconfig $INSTALLPREFIX/bin $INSTALLPREFIX/cmake &&
cp -rf include/GL $INSTALLPREFIX/include/ &&
cp -f lib/*.a $INSTALLPREFIX/lib/ &&
cp -f *.pc $INSTALLPREFIX/lib/pkgconfig/ &&
cp -f lib/*.dll src/*.exe $INSTALLPREFIX/bin/ &&
cp -f build/cmake/*.cmake $INSTALLPREFIX/cmake/ &&
echo Success

Build issue with go using SWIG and an external C library

I am trying to create a golang program which will use an external C library.
Before doing complex stuff, I wanted to test the use of SWIG in a small foo example.
I also want to be able to use the "go get" syntax without having to manually run swig. And to take into account that the external C library shall have been built before and can be located anywhere on the machine.
I am using golang 1.12.5 and swig 3.0.12
I have created the following structure:
.
|-- libfoo
| |-- foo.c
| |-- foo.h
| `-- libfoo.so
`-- src
|-- example_test.go
|-- lib.go
`-- libfoo.swig
In the libfoo folder I have a simple shared library. The files contain:
foo.c:
#include "foo.h"
int foo(int c){
return c+1;
};
foo.h
int foo(int c);
That I've compiled it with:
gcc -o ./libfoo.so -fPIC -shared ./foo.c
Then the golang files:
libfoo.swig
%module libfoo
%{
extern int foo(int a);
%}
extern int foo(int a);
lib.go
package libfoo
/*
#cgo LDFLAGS: -L../libfoo -lfoo
*/
import "C"
And finally example_test.go
package libfoo
import (
"testing"
"fmt"
)
func TestFoo(*testing.T) {
i := Foo(1)
fmt.Printf("%v\n",i)
}
When I try to build with "go build -x" It seems that the cgo directives located in the "lib.go" file are not taken into account as I do not see the flag appears in the output. Here is the output
06aa7e308d6:/mnt/data/src# go build -x
WORK=/tmp/go-build551116655
mkdir -p $WORK/b001/
swig -version
cd $WORK
/opt/go/pkg/tool/linux_amd64/compile -o ./b001/_go_.o -trimpath ./b001 -p main -complete -goversion go1.12.5 -D _$WORK -c=4 ./swig_intsize.go
cd /mnt/data/src
swig -go -cgo -intgosize 64 -module libfoo -o $WORK/b001/libfoo_wrap.c -outdir $WORK/b001/ libfoo.swig
CGO_LDFLAGS='"-g" "-O2"' /opt/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath _/mnt/data/src -- -I $WORK/b001/ -g -O2 ./lib.go $WORK/b001/_libfoo_swig.go
cd $WORK
gcc -fno-caret-diagnostics -c -x c - || true
gcc -Qunused-arguments -c -x c - || true
gcc -fdebug-prefix-map=a=b -c -x c - || true
gcc -gno-record-gcc-switches -c -x c - || true
cd $WORK/b001
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x002.o -c lib.cgo2.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x003.o -c _libfoo_swig.cgo2.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x004.o -c libfoo_wrap.c
TERM='dumb' gcc -I /mnt/data/src -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_cgo_main.o -c _cgo_main.c
cd /mnt/data/src
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o $WORK/b001/_x003.o $WORK/b001/_x004.o -g -O2
# _/mnt/data/src
/tmp/go-build551116655/b001/_x004.o: In function `_wrap_foo_libfoo_0f106433f15c8754':
/tmp/go-build/libfoo_wrap.c:255: undefined reference to `foo'
collect2: error: ld returned 1 exit status
If I set the CGO_LDFLAGS using an environment variable, it works. However I want to be able to just use "go get" without having those variables before.
I don't understand why go does not take my #cgo directive into account.
Did I miss something ?
Someone on the golang github repository has solved my issue.
The solution was to remove the newline between the import "C" directive and the comments containing the #cgo directives.

How do you assign the return value of a utility (grep/sed) to an array?

I am trying to create a shell script to make a Makefile. My strategy is to create two arrays, one containing all the .cpp files, and another with the .cpp suffixes replaces by .o. I then assign the .h and .ccp files from the return value of (grep | sed) to variable temp and then print the arrays. I keep getting the error however:
"syntax error: invalid arithmetic operator (error token is ".o"
My theory is that the return value for grep is not storing in temp correctly
I am very new to bash
cppfield=$(ls *.cpp)
x=0
for i in *.cpp; do
ofield[$x]="${i%.cpp}.o"
done
echo "$1 : ${ofield[#]}" > Makefile
echo " g++ -ansi -Wall -g -o $1 ${ofield[#]}" >> Makefile
for i in "${ofield[#]}"; do
temp=$(grep '#include "' ${cppfield[i]} | sed s/#include// | sed s/\"//g)
echo "${ofield[i]} : ${cppfield[i]} ${temp[#]}" >> Makefile
echo " g++ -ansi -Wall -g -c ${cppfield[i]}" >> Makefile
done
What you are trying to do is find the dependencies, which is probably better done by g++ -MM - it produces a dependency list for the file(s) you specified, that you can include/use in a makefile.
See the bottom of this file, my makefiles often contain something like this:
include .depends
.depends: Makefile ${SOURCES}
${CXX} -MM ${CXXFLAGS} ${SOURCES} > $#
Full file:
https://github.com/Leporacanthicus/lacsap/blob/master/Makefile
Your grep solution works reasonably well until you do something like:
#if _WIN32
#include <windows.h>
#else
#include <someother.h>
#endif
or
/* Don't need this any more...
#include "blah.h
*/
$ declare -a my_array
$ my_array=(`ls -1 *.cpp`)
$ echo ${my_array[0]}
a.cpp
$ echo ${my_array[1]}
b.cpp

how to link static library to makefile in linux

I have created a static library using database makefile project/database/Makefile like this
rm -rf libdatabase.a
database.o: database.cpp database.h
g++ -c -o database.o database.cpp
ar rvs libdatabase.a database.o
if [ -f libdatabase.a ];
then
echo "Database-Library Build Success"
cp libdatabase.a ../LIBs/
else
echo "databse-Library Build Failure"
fi
then i going to link it in main make file project/Main/Makefile like this
rm -rf testdate
INCPATH = -I../database
LIBPATH = -L../LIBs
testdate:main.o libdatabase.a
g++ -o testdate libdatabase.a main.o
main.o: main.cpp database.h
g++ -c -o main.o main.cpp
but its throwing error like :
make: * No rule to make target database.h', needed bymain.o'. Stop.
Plese some one coluld help me out of this. i am new to linux.
You have an extra backslash in your makefile at the end of the LIBPATH variable assignment. That's causing the next line (the testdate:... target) to be considered part of the variable value for LIBPATH. That in turn means the recipe line after that (the g++ -o testdate ... line) has no target defined, which is why you get that error.
Remove the backslash:
LIBPATH = -L../LIBs