Using makefile to link cimgui to project? - c++

I have a c project and I'm trying to link the C binding for imgui to it using Makefiles but I'm having some trouble. I've already generated the .dll using cmake in a build folder, these are the contents of it:
C:\path\to\cimgui\build:
-CMakefiles
-cimgui.dll
-cmake_install.txt
-CMakeCache.txt
-libcimgui.dll.a
-Makefile
and this is my makefile
PROJ := proj
TARGET := $(PROJ)
OBJ := sub.o main.o
INCLUDE_PATH := -IC:\path\to\SDL2 -IC:\path\to\cimgui
LIB_PATH := -LC:\path\to\SDL2\lib -LC:\path\to\cimgui\build
LINKER_FLAGS := -lSDL2 -lopengl32 -lcimgui -lcimgui.dll
CFLAGS := $(INCLUDE_PATH)
.PHONY : build clean
build : $(TARGET).exe
$(TARGET).exe: $(OBJ)
gcc $^ $(LIB_PATH) $(LINKER_FLAGS) -o $#
sub.o: sub.c sub.h
gcc -c $(CFLAGS) $< -o $#
main.o: main.c sub.h
$(CC) $(CFLAGS) $< -o $#
clean :
#rm -fv *.o
#rm -fv *.exe
and this is my main.c project
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_opengl.h>
int main(int argc, char ** argv)
{
return 0;
}
This is the error I get when i try to compile my makefile:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lcimgui.dll

Related

Dev-C++ 5.11 with MinGW have linking problem

I did try to compile a OpenCV-Project with MinGW (under Windows 10). The OpenCV-library had compiled without problem and I try to do couple of test.
main3.cpp
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
I have follows Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o
LINKOBJ = obj/main3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
All compiled and linked without mistakes nad run fine. I try to expand the test with new file 'second3.cpp'.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
extern "C" {
}
New the Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o obj/second3.o
LINKOBJ = obj/main3.o obj/second3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
obj/second3.o: second3.cpp
$(CPP) -c second3.cpp -o obj/second3.o $(CXXFLAGS)
After compiling I have follows list of mistakes:
- Command: mingw32-make.exe -f "C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win" all
g++.exe obj/main3.o obj/second3.o -o obj/Proj400-v3.exe -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
obj/main3.o:main3.cpp:(.text+0xc3): undefined reference to `cv::imread(std::string const&, int)'
obj/main3.o:main3.cpp:(.text+0x181): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2.exe: error: ld returned 1 exit status
C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win:25: recipe for target 'obj/Proj400-v3.exe' failed
mingw32-make.exe: *** [obj/Proj400-v3.exe] Error 1
Can some body explane me, what is wrong? Why with one source in the project it work fine und with many files in the project I have 'undefined reference'?
PS: If I change the project from two source-files to one-source, but do not remove the obj/second3.o, I have again problem with 'undefined reference'.

Why is the makefile returning g++: error: h file or directory make: *** [Makefile:12: test] Error 1?

I want to compile this program test.cpp
#include <iostream>
#include <cstring>
#include <iomanip>
#include <sodium.h>
#include <gmpxx.h>
#include <sstream>
using namespace std;
//Encryt or decrypt with RSA
mpz_class RSA(mpz_class m, mpz_class e,mpz_class N)
{
mpz_class rop;
mpz_powm(rop.get_mpz_t(), m.get_mpz_t(), e.get_mpz_t(), N.get_mpz_t());
return rop;
}
int main(const int argc, const char *const argv[])
{
if (argc!=4){
printf("usage: %s [Message] [Exponent] [Modulus] \n", argv[0]);
return 1;
}
const mpz_class m(argv[1]), d(argv[2]),N(argv[3]);
cout<<endl<<RSA(m,d,N);
return 0;
}
with this makefile Makefile
CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS = -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
$(CXX) -o $# $ˆ $(LDFLAGS)
%.o : %.cpp $(HDR)
$(CXX) $(CXXFLAGS) $< -o $#
clean :
rm -f $(OBJ) test
But I receive
-------------- Build: Debug in KA-RMP (compiler: GNU GCC Compiler)---------------
Checking if target is up-to-date: make -q -f Makefile Debug
Running command: make -f Makefile Debug
g++: error: h file or directory
make: *** [Makefile:12: test] Error 1
g++ -o test lsodium -lssl -lcrypto -lgmpxx
Process terminated with status 2 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Do you have any idea why I receive the error?
The problem is that $^ and $# is not working as you expected.
$# and $^ return the left and right values of $OBJ. However $OBJ simply resolves to "test.o".
OBJ = $(SRC:.cpp=.o) # converts test.cpp to test.o
Therefore $^ will return nothing because there is nothing on the right side.
If you want to use $^ you have to set OBJ to "test.o test.cpp". I made the changes with your makefile.
Otherwise use $(CXX) -o $# $(SRC) $(LDFLAGS).
CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS = -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC) $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
$(CXX) -o $# $^ $(LDFLAGS)
%.o : %.cpp $(HDR)
$(CXX) $(CXXFLAGS) $< -o $#
clean :
rm -f $(OBJ) test

function define in .h declared in .cu

I made a project split in three folders : src, include, obj
I am using CImg.h and cuda.h library.
I declare void convolve(cimg_library::CImg<float>&, cimg_library::CImg<float> const &);in convolve.h
and define it in convolve.cu
my files :
main.cpp :
#include "CImg.h"
#include "../include/convolve.h"
using namespace cimg_library;
int main(){
CImg<float> var1("/*path*/");
CImg<float> var2("/*path2*/");
convolve(var1,var2);
//some code
}
convolve.h :
1 #ifndef CONVOLVE_H
2 #define CONVOLVE_H
//some define
10 void convolve(cimg_library::CImg<float>&, cimg_library::CImg<float> const &);
11 #endif //CONVOLVE_H
convolve.cu :
1 void convolve(CImg<float>& img, const CImg<float>& kernel){
//some code
24 kernel<<<dimGrid,dimBlocks>>>(/*some arg*/);
}
and my new makefile :
1 CC=nvcc
2 CX=g++
3 IDIR =../include
4 special_IDIR = /usr/local/cuda-9.0/include
5 LDIR = /usr/local/cuda-9.0/lib64
6 CFLAGS=-I$(special_IDIR) -L$(LDIR)
7
8 LIBS = -lX11 -lpthread -lcudart
9 ODIR = ../obj
10
11
12 _DEPS = convolve.h kernel.cuh
13 DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
14
15 _OBJ = main.o convolve.o kernel.o
16 OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
17
18 $(ODIR)/%.o: %.cpp $(DEPS)
19 $(CX) -x c++ -o $# $< $(CFLAGS) $(LIBS)
20
21 $(ODIR)/%.o: %.cu $(DEPS)
22 $(CC) -cu -o $# $< $(CFLAGS)
23
24 all: $(OBJ)
25 $(CC) -o $# $^ $(CFLAGS)
26
27
28 .PHONY: clean
29
30 clean:
31 rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
Here is my issue : it says the reference to void convolve(cimg_library::CImg<float>&, cimg_library::CImg<float> const &) is undefined.
I think this is because I never clearly say to the compiler that the definition of convolve is in convolve.cu. But I do not find how to make this link properly.
Thanks for your help !
please don't post code (or Makefile contents) with line numbers. It just gets in the way, usually.
The compilation process here follows a compile...link process. The first stage is the compile stage, and you should be using -c for both g++ and nvcc during this stage (i.e. for both types of Makefile targets)
nvcc has no -cu option. I think maybe you meant -x cu
I also removed the ampersands on the function call.
Here is a simplified example based on what you have shown, primarily removing the CImg stuff and some other unnecessary items:
$ cat main.cpp
#include "convolve.h"
int main(){
int var1 = 0;
int var2 = 1;
convolve(var1, var2);
}
$ cat convolve.h
void convolve(int &, int &);
$ cat convolve.cu
#include "convolve.h"
__global__ void kernel(){};
void convolve(int & i1, int & i2){
kernel<<<1,1>>>();
}
$ cat Makefile
CC=nvcc
CX=g++
IDIR = .
special_IDIR = /usr/local/cuda-9.1/include
LDIR = /usr/local/cuda-9.1/lib64
CFLAGS=-I$(special_IDIR) -L$(LDIR)
LIBS =
ODIR = .
_DEPS = convolve.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = main.o convolve.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cu $(DEPS)
$(CC) -c -x cu -o $# $< $(CFLAGS)
$(ODIR)/%.o: %.cpp $(DEPS)
$(CX) -c -x c++ -o $# $< $(CFLAGS) $(LIBS)
all: $(OBJ)
$(CC) -o $# $^ $(CFLAGS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
$ make clean
rm -f ./*.o *~ core /*~
$ make
g++ -c -x c++ -o main.o main.cpp -I/usr/local/cuda-9.1/include -L/usr/local/cuda-9.1/lib64
nvcc -c -x cu -o convolve.o convolve.cu -I/usr/local/cuda-9.1/include -L/usr/local/cuda-9.1/lib64
nvcc -o all main.o convolve.o -I/usr/local/cuda-9.1/include -L/usr/local/cuda-9.1/lib64
$
change the instances of cuda-9.1 to cuda-9.0 in the above Makefile if you have CUDA 9.0 in your setup. The linker libs (-L) are not really needed for the compile commands, however. When linking with nvcc as we are doing here, it's also unnecessary to pass the -I and -L switches that refer to the CUDA includes and CUDA libraries that are part of the toolkit. nvcc already knows how to find those.
There may be any number of additional recommendations or tweaks to the Makefile, for instance the -x cu and -x c++ switches are probably unnecessary (at least they are unnecessary for my simplified example) but my objective here is not to create the perfect Makefile, but to give you a roadmap to get past the issue you are currently witnessing.

Linker errors when trying to statically linking Boost

I am trying to include a Boost library in my program, but I am having difficulties statically linking my program. I get a bunch of linker errors even though I have added -L/usr/include/boost/ -lboost_filesystem to my makefile.
E.g., during compilation I get undefined reference to boost::iostreams::detail::gzip_footer::reset()'
My version of Boost is 1.61.0.2, I am running Ubuntu 16.10 (64 bit) and gcc version 6.2.0 20161005. My boost libraries such as accumulators, algorithms, ... are located in /usr/include/boost, so my makefile looks like this:
CXX = g++
CXXFLAGS = -static -std=c++11 -Wall
LDFLAGS = -L/usr/include/boost/ -lboost_filesystem
DEPFLAGS = -MM
SRC_DIR = ./src
OBJ_DIR = ./obj
SRC_EXT = .cpp
OBJ_EXT = .o
TARGET = main
SRCS := $(wildcard $(SRC_DIR)/*$(SRC_EXT))
OBJS := $(SRCS:$(SRC_DIR)/%$(SRC_EXT)=$(OBJ_DIR)/%$(OBJ_EXT))
DEP = depend.main
.PHONY: clean all depend
all: $(TARGET)
$(TARGET): $(OBJS)
#echo "-> linking $#"
#$(CXX) $^ $(LDFLAGS) -o $#
$(OBJ_DIR)/%$(OBJ_EXT) : $(SRC_DIR)/%$(SRC_EXT)
#echo "-> compiling $#"
#$(CXX) $(CXXFLAGS) -o $# -c $<
clean:
#echo "removing objects and main file"
#rm -f $(OBJS) $(TARGET) *.out
$(SRC_DIR)/%.$(SRC_EXT):
$(CXX) $(DEPFLAGS) -MT \
"$(subst $(SRC_DIR),$(OBJ_DIR),$(subst $(SRC_EXT),$(OBJ_EXT),$#))" \
$(addprefix ,$#) >> $(DEP);
clear_dependencies:
#echo "-> (re-)building dependencies";
#$(RM) $(DEP)
depend: clear_dependencies $(SRCS)
-include $(DEP)
I'm trying to compile the following file (an example found online)
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
namespace bo = boost::iostreams;
int main()
{
{
std::ofstream ofile("hello.gz", std::ios_base::out | std::ios_base::binary);
bo::filtering_ostream out;
out.push(bo::gzip_compressor());
out.push(ofile);
out << "This is a gz file\n";
}
{
std::ifstream ifile("hello.gz", std::ios_base::in | std::ios_base::binary);
bo::filtering_streambuf<bo::input> in;
in.push(bo::gzip_decompressor());
in.push(ifile);
boost::iostreams::copy(in, std::cout);
}
}
Your program does not use libboost_filesystem at all. The only
boost library it depends on is liboost_iostreams.

The executable test file is not running the tests

I have created a project:
// func.h :
#ifndef FUNCS_H_
#define FUNCS_H_
int addInts(int i, int j);
#endif /* FUNCS_H_ */
// func.cpp
#include "func.h"
int addInts(int i, int j) {
return i + j;
}
// main.cpp
#include "func.h"
#include <iostream>
int main() {
std::cout << addInts(5, 7) << std::endl;
return 0;
}
//makefile
OBJS := \
main.o \
func.o
CXX := g++
CXX_FLAGS := -Wall -Werror -Wextra -std=c++11
all: main_prj
main_prj: $(OBJS)
$(CXX) $(OBJS) -lm -o main
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $*.cpp -o $*.o
clean:
rm -f main $(OBJS)
And I created also a test (boost test) for that function:
// test/main_test.cpp
#define BOOST_TEST_MODULE main_test
#include <boost/test/included/unit_test.hpp>
//____________________________________________________________________________//
// FIXTURES ...
//____________________________________________________________________________//
// test/addInts/addInts_test.cpp
#include <boost/test/unit_test.hpp>
#include "../../func.h"
BOOST_AUTO_TEST_CASE(addInts_5_7) {
int addInts_5_7 = 5 + 7;
BOOST_CHECK_EQUAL(addInts_5_7, addInts(5, 7));
}
// test/makefile
OBJS_TEST := \
main_test.o \
addInts/addInts_test.o \
../func.o
CXX_TEST := g++
CXX_FLAGS_TEST := -Wall -Werror -Wextra -std=c++11
all_test: main_test_prj
main_test_prj: $(OBJS_TEST)
$(CXX_TEST) $(OBJS_TEST) -lm -o main_test
-include $(OBJS_TEST:.o=.d)
%.o: %.cpp
$(CXX_TEST) -c $(CXX_FLAGS_TEST) $*.cpp -o $*.o
clean_test:
rm -f main_test $(OBJS_TEST)
Now, the make commands work, so they clean all the created files, or create the o files and the executables. When I run the main file, the output is correct: 12 ; but when I run the main_test file, the output is a little strange:
Running 1 test case...
*** No errors detected
I would expect the output with running tests and OK/KO, or pass/not pass... I cannot figure what am I doing wrong. Can anyone help me, please?
You have one test.
The output says that one test was run, and that no errors were found.
This output appears to be as documented.
There is no problem here.
Though, sadly, the documentation on how to change this output is rather lacking…