0

Makefile specified in this question, compiling all the cpp programs in a folder but not with python embedded cpp programs.

all: myUB

sourcesC := $(wildcard ../src/*.cpp)

objectsC := $(patsubst %.cpp,%.o,$(sourcesC))

INPATH=-I"C:/Python27/include"

LIBPATH=-L"C:/Python27/libs"-lpython27

myUB:

    @echo 'Building target $@'

    g++ -O0 -Wall -c -g3 -fmessage-length=0 \
        $(sourcesC)

    del *.o

clean:
code_fodder
  • 15,263
  • 17
  • 90
  • 167
Alex
  • 35
  • 6
  • There are a few issues here. I don't think you should have new lines between the lines of the `myUB` recipie. Also it looks like you are trying to compile all the source files in one go (i.e. not in one object file per source file). You can do this, but it looks like you are expecting multiple object files? You did not specify an output file in the `g++` line (using `-o one_obj_fits_all.o`). What output do you get and what are you exprecting and what specific problem do you have? – code_fodder Aug 13 '19 at 17:25
  • Hi, thank you for quick reply. I'm getting following errors- ../src/ub0013.cpp:95: error: `Py_Initialize' was not declared in this scope ../src/ub0013.cpp:96: error: `PyRun_SimpleString' was not declared in this scope ../src/ub0013.cpp:99: error: `Py_Finalize' was not declared in this scope I want compile all the python program embedded in cpp using make. For this we have to specify path of python include and libs folder in our g++ command, but I don't know how to do that . – Alex Aug 13 '19 at 17:44
  • The following command is giving me expected output for single source file. g++ -Wall -IC:\Python27\include -LC:\Python27\libs -o python_test.exe test1.cpp -lpython27 But I want use it in make file to compile all the source file. – Alex Aug 13 '19 at 17:45
  • so why don't you add that to your makefile `g++` line: `g++ -O0 -Wall -c -g3 -fmessage-length=0 -o some_obj.o $(sourcesC) -IC:\Python27\include -LC:\Python27\libs -lpython27`? Note: that this compiles all the files together into one single object called some_obj.o... – code_fodder Aug 13 '19 at 17:49
  • What do you want at the end of all this? an object? a library? or an executable? - each variant requires slightly different handling... – code_fodder Aug 13 '19 at 17:51
  • executable(.out file for all the source file) – Alex Aug 13 '19 at 17:53
  • In that case remove the `-c` flag (compile only) then it will compile and link into an executable. Change the `-o` flag (output filename) to somthing like: `-o my_program.out` – code_fodder Aug 13 '19 at 18:01

1 Answers1

0

Your final makefile could look somthing like:

all: myUB

sourcesC := $(wildcard ../src/*.cpp)

# Not used
#objectsC := $(patsubst %.cpp,%.o,$(sourcesC))

INC = -IC:\Python27\include

LIBS = -LC:\Python27\libs -lpython27

myUB:
    @echo 'Building target $@'
    g++ -O0 -Wall -g3 -fmessage-length=0 -o myprog.out $(sourcesC) $(INC) $(LIBS)

clean:
    rm myprog.out

update

For the undefined ref to WinMain(), it means the linker can't find this function in your code. Either you need to include a library/object that contains it or you can define it yourself in a cpp file like:

#include <windows.h>

int WINAPI (*MyDummyReferenceToWinMain)(HINSTANCE hInstance, ..., int
nShowCmd ) = &WinMain;

I got the function template from here.

But this seems to mean that you are creating a windows application instead of a console app which uses int main(...) entry point.

Update2

I have made a new makefile to do what you have asked for in your latest comment which seems to be to create one executable per source file - I am assuming each source file has its own main.

# Build vars
CXX = g++
CXX_FLAGS = -O0 -Wall -g3
INC = -IC:\Python27\includ
LIBS = -LC:\Python27\libs -lpython27

# Sources
SRC_DIR=src
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
$(info SOURCES: $(SOURCES))

# Executables
EXE_DIR=bin
EXECUTABLES = $(subst $(SRC_DIR)/,$(EXE_DIR)/,$(subst cpp,out,$(SOURCES)))
$(info EXECUTABLES: $(EXECUTABLES))

$(info ----)

# Directories
DIRS = $(EXE_DIR)

# Rule to create folders and compile executables
all: $(DIRS) $(EXECUTABLES)

# Pattern rule to build each executable
$(EXE_DIR)/%.out : $(SRC_DIR)/%.cpp
    @echo "compiling $< --> $@"
    @$(CXX) $(CXX_FLAGS) -o $@ $< $(INC) $(LIBS)

# Rule to create output dirs
$(DIRS):
    @echo "Creating output folders"
    @mkdir -p $(EXE_DIR)

# Rule to clean up
clean:
    @echo "Cleaning"
    @rm -rf $(EXE_DIR)

This should create one executable in the folder bin/ for each source file (.cpp) in folder src/.

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • hi, it's working perfectly. But what I should do get different out file for each cpp program . Thank you so much for helping me out. – Alex Aug 17 '19 at 19:12
  • I'm getting following error, while compiling multiple cpp files with make. sdk/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16' collect2: ld returned 1 exit status make: *** [myUB] Error 1 @code_fodder – Alex Aug 17 '19 at 19:29
  • Its a windows thing, I think you need to add a function `WinMain()` in your program but I am not 100% sure - see the update I added (in a sec)... – code_fodder Aug 17 '19 at 19:32
  • ok now it's compiling all the cpp file but not generating any out file with following error- C:\Local\Temp/ccNWFTnI.o:apicppCopy.cpp:(.text+0x0): multiple definition of `main' C:\Local\Temp/ccobcNwv.o:apicpp.cpp:(.text+0x0): first defined here collect2: ld returned 1 exit status make: *** [myUB] Error 1 – Alex Aug 17 '19 at 19:36
  • That looks like you have a copy of the same file!? - meaning it will have multiple of everything in it? – code_fodder Aug 17 '19 at 19:39
  • the makefile code that you gave me is for multiple cpp programs with one main function. I know it's too much confusing but till now whatever you suggested working perfectly. But I want to use your makefile to compile all cpp files each having it's own main function and outfile. could you please edit your code with this - %.o: %.cpp Makefile g++ -O0 -Wall -g3 -IC:\Python27\include -LC:\Python27\libs $< -o $@ -lpython27 – Alex Aug 17 '19 at 23:24
  • oh - you mean each cpp file compiles into a stand alone executable? So, just to get this straight, if you have 5 .cpp files you want to end up with 5 individual executables? If so, yes that is also easy and pretty much like what you wrote. I will take make a little example one in my VM later and post it up for you... – code_fodder Aug 18 '19 at 09:07
  • Hey, Thank you so much for helping me out.@code_fodder – Alex Aug 31 '19 at 16:24