-1

I added some c code into a c++ code base on a windows machine; it was working on windows visual studio fine; but I'm having a hard time trying to get it to run on linux.

Below file is a subdir.mk that is run with a makefile. I'm editing this file written for purely c++ on a linux64 system to add my c changes. I added cc sources as you can see below. I'm new at this. I heared that if you have flags make knows how to build but the existing file had this rule %.o: ../%.cpp and it won't work without it. I tried adding %.o: ../%.c in different ways (with a , and then by piping) but was not successful.

Do you see what I'm doing wrong here? Any feedback welcome. Thx!

CC=gcc
CXX=g++

CXXFLAGS=-Wall -g
CCFLAGS=-g

# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../x.cpp \
../y.cpp \
../main.cpp

CC_SRCS += \
../z__c.c

OBJS += \
./x.o \
./y.o \
./z__c.o \
./main.o

CPP_DEPS += \
./x.d \
./y.d \
./main.d

icpc='/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/bin/g++'
CC='/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/bin/gcc'
# Each subdirectory must supply rules for building sources it contributes
%.o: ../%.cpp
#%.o: ../%.c


    @echo 'Building file: $<'
    @echo 'Invoking: G++ Compiler'
    g++ -Wall -mavx -g -O -O1 -std=c++11 -L/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/lib64 -libstdc -libsupc++ -libtsan -libubsan -libvtv -libquadmath -libgcc_s -libgcj  -L/opt/intel/composer_xe_2013.5.192/mkl/lib/intel64 -lmkl_mc -lmkl_sequential -lmkl_gf_ilp64 -lmkl_avx -lmkl_lapack95_lp64 -lmkl_intel_lp64 -lmkl_core -lmkl_gnu_thread -L/home../oed -liboed -liboedlib -I/opt/intel/composer_xe_2013.5.192/mkl/include  
    -I/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/include -I/home..include -I/home../debug  -DDEBUG -DMKL_ILP64
    -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -c -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '
MadHatter
  • 321
  • 7
  • 17
  • Could it be the blank line after `./x.d`? – Garmekain Jul 18 '17 at 20:40
  • 1
    Where are you learning how to write makefiles from? –  Jul 18 '17 at 20:42
  • I'm reading through GNU make manual- which is not easy to understand, but this is not written by me originally. It seems to be generated by eclipse and I'm only editing to make the changes in code work on this environment(linux). – MadHatter Jul 18 '17 at 20:43
  • These rules are [built-in](https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules) for GNU. Chances are you could remove them without ill effect. – CAB Jul 18 '17 at 20:48
  • I removed those and ran make, it didn't work. (%.o: ../%.cpp) But without the c additions (just c++) it creates the library without a problem. – MadHatter Jul 18 '17 at 20:50
  • [posix make](http://pubs.opengroup.org/onlinepubs/009695399/utilities/make.html) doesn't seem to use the percent signs. Scroll about half way down to "Default Rules", – CAB Jul 18 '17 at 20:53
  • 1
    I notice the `../%.cpp`. I think that complicates it a bit. You might want to read [How to place object files in separate subdirectory](https://stackoverflow.com/q/5178125/2800918) – CAB Jul 18 '17 at 20:58
  • I'll have a look. Thx. – MadHatter Jul 18 '17 at 21:09
  • 1
    Actually the GNU make manual is one of the best examples of technical writing around - your problem is trying to use eclipse for C++ programming, or anything really. –  Jul 18 '17 at 22:02

1 Answers1

2

Don't you want something like this?

CC=gcc
CXX=g++

CXXFLAGS=-Wall -g
CCFLAGS=-g

# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
../x.cpp \
../y.cpp \
../main.cpp

CC_SRCS += \
../z__c.c

OBJS += \
./x.o \
./y.o \
./z__c.o \
./main.o

CPP_DEPS += \
./x.d \
./y.d \
./main.d

icpc='/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/bin/g++'
CC='/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/bin/gcc'
# Each subdirectory must supply rules for building sources it contributes
%.o: ../%.cpp
    @echo 'Building file: $<'
    @echo 'Invoking: G++ Compiler'
    g++ -Wall -mavx -g -O -O1 -std=c++11 -L/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/lib64 -libstdc -libsupc++ -libtsan -libubsan -libvtv -libquadmath -libgcc_s -libgcj  -L/opt/intel/composer_xe_2013.5.192/mkl/lib/intel64 -lmkl_mc -lmkl_sequential -lmkl_gf_ilp64 -lmkl_avx -lmkl_lapack95_lp64 -lmkl_intel_lp64 -lmkl_core -lmkl_gnu_thread -L/home../oed -liboed -liboedlib -I/opt/intel/composer_xe_2013.5.192/mkl/include  
    -I/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/include -I/home..include -I/home../debug  -DDEBUG -DMKL_ILP64
    -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -c -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '

%.o: ../%.c
    @echo 'Building file: $<'
    @echo 'Invoking: GCC Compiler'
    gcc -Wall -mavx -g -O -O1 -L/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/lib64 -libstdc -libsupc++ -libtsan -libubsan -libvtv -libquadmath -libgcc_s -libgcj  -L/opt/intel/composer_xe_2013.5.192/mkl/lib/intel64 -lmkl_mc -lmkl_sequential -lmkl_gf_ilp64 -lmkl_avx -lmkl_lapack95_lp64 -lmkl_intel_lp64 -lmkl_core -lmkl_gnu_thread -L/home../oed -liboed -liboedlib -I/opt/intel/composer_xe_2013.5.192/mkl/include  
    -I/opt/shared/sw/x86_64-unknown-linux-gnu/gcc/5.3.0-rhel6/include -I/home..include -I/home../debug  -DDEBUG -DMKL_ILP64
    -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -c -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '
cleblanc
  • 3,678
  • 1
  • 13
  • 16
  • `subdir.mk:59: warning: overriding recipe for target '@echo' ` `subdir.mk:53: warning: ignoring old recipe for target '@echo' ` `subdir.mk:59: warning: overriding recipe for target ''Building' ` `subdir.mk:53: warning: ignoring old recipe for target ''Building' ` `subdir.mk:59: warning: overriding recipe for target 'file' ` `subdir.mk:53: warning: ignoring old recipe for target 'file' ` `make: *** No rule to make target ''', needed by '@echo'. Stop. ` – MadHatter Jul 18 '17 at 21:14
  • 1
    @MadHatter I just reformatted this and saved it as a makefile (removing the leading spaces and inserting tabs and it's working with make `GNU Make 4.2.1 Built for i686-pc-cygwin Copyright (C) 1988-2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.` – cleblanc Jul 19 '17 at 14:30
  • make formatting is really tricky;spent a lot of time on this; wish I knew that before. Finally got it working thanks to you. @cleblanc – MadHatter Jul 19 '17 at 15:15