I am relatively new to go, and I am having some trouble with the go build system.
GO Environment:
(base) ngadre-mbp:github.com ngadre$ go version
go version go1.14.5 darwin/amd64
(base) ngadre-mbp:github.com ngadre$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ngadre/Library/Caches/go-build"
GOENV="/Users/ngadre/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ngadre/Desktop/Workspace/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5g/59l0__050lg6p4hv1wv7mhw40000gn/T/go-build110012391=/tmp/go-build -gno-record-gcc-switches -fno-common"
My directory structure is as below:
pwd: /Users/ngadre/Desktop/Workspace/go/src/github.com
.
├── coredhcp
│ ├── LICENSE
│ ├── README.md
│ ├── cmds
│ │ ├── client
│ │ ├── coredhcp
│ │ └── coredhcp-generator
│ ├── config
│ │ ├── config.go
│ │ ├── config_test.go
│ │ └── errors.go
│ ├── go.mod
│ ├── go.sum
│ ├── handler
│ │ └── handler.go
│ ├── logger
│ │ └── logger.go
│ ├── plugins
│ │ ├── dns
│ │ ├── example
│ │ ├── file
│ │ ├── leasetime
│ │ ├── nbp
│ │ ├── netmask
│ │ ├── plugin.go
│ │ ├── range
│ │ ├── router
│ │ └── serverid
│ └── server
│ ├── handle.go
│ └── serve.go
├── go-immutable-radix
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── edges.go
│ ├── go.mod
│ ├── go.sum
│ ├── iradix.go
│ ├── iradix_test.go
│ ├── iter.go
│ ├── node.go
│ └── raw_iter.go
Both the projects "coredhcp" and "go-immutable-radix" I have cloned using git clone
I did some changes to the "go-immutable-radix" project and added logs, however, I am not able
to build my local changes into the "coredhcp" project which uses this "go-immutable-radix" project.
go build cmds/coredhcp/main.go
Any suggestions how I can compile local changes in "A" project into "X" project through go build.
If you have a locally cloned version of another go module, use the replace directive in the go.mod (the go.mod for the coredhcp package) to point the local copy:
replace github.com/.../go-immutable-radix => ../go-immutable-radix
Related
I have a file, example.go that I want to include in my main.go file. But my IDE and go tool are both complaining when I try to do this.
VS Code Error:
could not import example (cannot find package "example" in any of
/usr/local/go/src/example (from $GOROOT)
/root/go/src/example (from $GOPATH))
When I run go run cmd/my_project/main.go I get the following error from Go tool:
cmd/my_project/main.go:4:2: package example is not in GOROOT (/usr/local/go/src/example)
My project structure is as follows.
pet_project
├── cmd
│ └── my_project
│ ├── example
│ │ ├── example.go
│ │ ├── hello.pb.cc
│ │ ├── hello.pb.h
│ │ └── hello_wrap.cxx
│ ├── main.go
│ └── main_test.go
├── go.mod
├── go.sum
Contents of go.mod:
module my_project
go 1.19
require google.golang.org/protobuf v1.28.1
require (
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/text v0.5.0 // indirect
)
The contents of the main.go file, in this file I'm trying to call the code in example.go.
package main
import (
"example"
"fmt"
)
func main() {
var p example.Person
p.Dob = 20
example.Handle(p)
}
The contents of example.go are:
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (https://www.swig.org).
* Version 4.1.1
*
* Do not make changes to this file unless you know what you are doing - modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
// source: hello.i
package example
/*
#define intgo swig_intgo
typedef void *swig_voidp;
#include <stddef.h>
#include <stdint.h>
... omitted for brevity
Below are the Go environment variables:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build578899875=/tmp/go-build -gno-record-gcc-switches"
I have tried running go install or go build and they both fail with the above error.
I have also changed the import of to "cmd/my_project/example" and ran go mod tidy but still face the same error.
I am trying to build ZynAddSubFX using CMake, and it errored:
D:/work/zyn-fusion-build/src/zynaddsubfx/rtosc/src/rtosc.c:9:10: fatal error: rtosc/rtosc.h: No such file or directory
9 | #include <rtosc/rtosc.h>
| ^~~~~~~~~~~~~~~
compilation terminated.
This is the command (simplified):
gcc #CMakeFiles/rtosc.dir/includes_C.rsp -O2 -fPIC -std=c99 -Wall -Wextra -MD -MT rtosc/CMakeFiles/rtosc.dir/src/rtosc.c.obj -MF CMakeFiles/rtosc.dir/src/rtosc.c.obj.d -o CMakeFiles/rtosc.dir/src/rtosc.c.obj -c /g/Projects/zyn-fusion-build/src/zynaddsubfx/rtosc/src/rtosc.c
File system tree:
.
├── CMakeFiles
│ ├── CMakeDirectoryInformation.cmake
│ └── rtosc.dir
│ ├── DependInfo.cmake
│ ├── build.make
│ ├── cmake_clean.cmake
│ ├── cmake_clean_target.cmake
│ ├── compiler_depend.make
│ ├── compiler_depend.ts
│ ├── depend.make
│ ├── flags.make
│ ├── includes_C.rsp
│ ├── link.txt
│ ├── progress.make
│ └── src
├── CTestTestfile.cmake
├── Makefile
└── cmake_install.cmake
I can cat (command) CMakeFiles/rtosc.dir/includes_C.rsp, and if I replace #CMakeFiles/rtosc.dir/includes_C.rsp to the file's content, it will not make any error.
I can't edit Makefile directly because CMake will regenerate all Makefiles.
What should I do to solve this problem?
Edit 1:
CMakeLists.txt example:
add_library(rtosc src/rtosc.c src/dispatch.c src/rtosc-time.c)
target_include_directories(rtosc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
includes_C.rsp example:
-I"/g/Projects/zyn-fusion-build/src/zynaddsubfx/rtosc/include"
gcc --version:
$ gcc --version
gcc.exe (Rev4, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I am trying to run my makefile using make run but its getting some errors : "No rule to make target 'put.h', needed by 'straps.o'. Stop". The error is about GCC can't find the headers file(put.h) when running makefile with the text below :
In kernel directory's Makefile
VPATH=../../include
all : straps.o entry.o head.o
straps.o : straps.c put.h
$(GCC) -c straps.c $(CFLAG)
entry.o : entry.S
$(GCC) -c entry.S $(CFLAG)
head.o : head.S
$(GCC) -c head.S $(CFLAG)
(straps.c include"put.h" so i need to include put.h)
I am trying to include headers file in the gcc command line (by using $(GCC) -c straps.c $(CFLAG) -I../include) but still not working. Can anyone explain why I am getting this error and suggest a fix solution if possible. Thank you
My file list
Linux
├── arch
│ └── riscv
│ ├── kernel
│ │ ├── entry.S
│ │ ├── head.S
│ │ ├── Makefile
│ │ ├── straps.c
│ │ └── vmlinux.lds
│ └── Makefile
├── include
│ ├── put.h
│ └── test.h
├── init
│ ├── main.c
│ ├── Makefile
│ └── test.c
├── lib
│ ├── Makefile
│ └── put.c
└── Makefile
Main Makefile gives the variable as below :
export
CROSS_= riscv64-unknown-elf-
AR=${CROSS_}ar
GCC=${CROSS_}gcc
LD=${CROSS_}ld
OBJCOPY=${CROSS_}objcopy
ISA ?= rv64imafd
ABI ?= lp64
INCLUDE = -I ../include
CF = -g -O3 -march=$(ISA) -mabi=$(ABI) -mcmodel=medany -ffunction-sections -fdata-sections -nostartfiles -nostdlib -nostdinc -static -lgcc -Wl,--nmagic -Wl,--gc-sections
CFLAG = ${CF} ${INCLUDE}
***Solution :
The kernel directory's Makefile Updated as below (refers to the answer given in the post below):
VPATH = ../../../include
CFLAG += -I../../../include
all : straps.o entry.o head.o
straps.o : straps.c put.h
$(GCC) -c straps.c $(CFLAG)
entry.o : entry.S
$(GCC) -c entry.S $(CFLAG)
head.o : head.S
$(GCC) -c head.S $(CFLAG)
I think that you're assuming that VPATH is somehow related to how the compiler locates header files. That is definitely not the case. VPATH is a make construct, and controls where make looks for prerequisites of targets appearing in the makefile.
It has absolutely nothing to do with where the compiler looks to find preprocessor include files. To control that you have to add -I options to the compile line.
So for example use:
VPATH = ../../../include
CFLAG += -I../../../include
The dependency list following "straps.o:" must refer to objects you have rules to actualy make within the makefile. In that makefile you have rules to make straps.o, entry.o, and head.o, but not put.h. Or straps.c for that matter.
I'm using the following vpath to attempt locating my $(OBJ) files:
vpath %.o ./lib/obj
And my target is setup as such:
# Link target
link:
#echo "\nLinking files"
$(CC) $(LINK_FLAGS) -o main.elf $(OBJS)
When looking at the output, I get (for all the *.o files):
...error: misc.o: No such file or directory
My project structure looks as follows:
.
├── Makefile
├── inc
│ └── main.h
├── lib
│ ├── inc
│ │ ├── cmsis
│ │ │ ├── arm_common_tables.h
│ │ │ ├── ...
│ │ ├── peripherals
│ │ │ ├── misc.h
│ │ │ ├── ...
│ │ └── stm32f4xx
│ │ ├── stm32f4xx.h
│ │ ├── ...
│ ├── obj
│ │ ├── misc.o
│ ├── src
│ │ ├── peripherals
│ │ │ ├── misc.c
│ │ │ ├── ...
│ │ └── system_stm32f4xx.c
│ └── startup_stm32f4xx.s
├── src
│ └── main.c
└── stm32f4.ld
Why are my .o files not being found?
Full output for reference:
arm-none-eabi-gcc -T"stm32f3.ld" -nostartfiles -Wl,-Map,"main.map"
-mcpu=cortex-m4 -mthumb -g3 -gdwarf-2 -L"./" -o main.elf misc.o stm32f4xx_adc.o stm32f4xx_can.o stm32f4xx_crc.o stm32f4xx_cryp.o
stm32f4xx_cryp_aes.o stm32f4xx_cryp_des.o stm32f4xx_cryp_tdes.o
stm32f4xx_dac.o stm32f4xx_dbgmcu.o stm32f4xx_dcmi.o stm32f4xx_dma.o
stm32f4xx_exti.o stm32f4xx_flash.o stm32f4xx_fsmc.o stm32f4xx_gpio.o
stm32f4xx_hash.o stm32f4xx_hash_md5.o stm32f4xx_hash_sha1.o
stm32f4xx_i2c.o stm32f4xx_iwdg.o stm32f4xx_pwr.o stm32f4xx_rcc.o
stm32f4xx_rng.o stm32f4xx_rtc.o stm32f4xx_sdio.o stm32f4xx_spi.o
stm32f4xx_syscfg.o stm32f4xx_tim.o stm32f4xx_usart.o stm32f4xx_wwdg.o
system_stm32f4xx.o
$(OBJS) is just a variable, and gets expanded as text. When it appears in the text of the command in your link rule, it is simply expanded as text.
vpath searches are applied to prerequisites, so you will have to arrange for your $(OBJS) to appear as prerequisites to your link rule and for the rule's commands to use those prerequisites (via automatic variables, rather than just using $(OBJS) directly).
So your rule needs to look more like
# Link target
link: main.elf
main.elf: $(OBJS)
#echo "\nLinking files"
$(CC) $(LINK_FLAGS) -o $# $+
(I have also taken the liberty of writing out the file generated (main.elf) as the non-phony target of an intermediate rule. Making these things explicit to Make is generally a good idea.)
Although the other answers are correct in the details of what they say, there is a larger point here: it is not possible to use VPATH/vpath reliably to find derived files, like .o files that you build from your makefile. VPATH/vpath is only useful for finding source files, such as .c files.
You should read http://make.mad-scientist.net/vpath.html
vpath just tells make where to find the files, not the compiler/linker. You have to get make to tell the compiler/linker where it found them. The easiest way is to make them proper dependencies (so they get rebuilt before linking) and then use $^:
# Link target
link: $(OBJS)
#echo "\nLinking files"
$(CC) $(LINK_FLAGS) -o main.elf $^
I've added the following to my Makefile:
OBJS = $(LIB_SRC:.c=.o)
OBJ_FILES = $(addprefix lib/out/,$(notdir $(LIB_SRC:.c=.o)))
and updated the target:
link:
#echo "\nLinking files"
$(CC) $(LINK_FLAGS) -o main.elf $(OBJ_FILES)
The linker is now finding the .o files correctly.
I'm trying to write a Makefile that will build a list of .o files from source, and then in a separate make target, setup linking etc. My Makefile currently looks like this:
CC=arm-none-eabi-gcc
vpath %.c src src/peripherals
vpath %.o out
OUT_DIR = out
CFLAGS = -DUSE_STDPERIPH_DRIVER
CFLAGS += -c -fmessage-length=0 -g3 -gdwarf-2 -O0 -Wall -Wa,-adhlns="$#.lst"
CFLAGS += -mthumb -mcpu=cortex-m4
CFLAGS += -MMD -MP -MF"$#.d" -MT"$#.d"
CFLAGS += -Iinc -Iinc/cmsis -Iinc/peripherals -Iinc/stm32f4xx
SRC = misc.c stm32f4xx_adc.c stm32f4xx_can.c stm32f4xx_crc.c stm32f4xx_cryp.c stm32f4xx_cryp_aes.c \
stm32f4xx_cryp_des.c stm32f4xx_cryp_tdes.c stm32f4xx_dac.c stm32f4xx_dbgmcu.c stm32f4xx_dcmi.c stm32f4xx_dma.c \
stm32f4xx_exti.c stm32f4xx_flash.c stm32f4xx_fsmc.c stm32f4xx_gpio.c stm32f4xx_hash.c stm32f4xx_hash_md5.c \
stm32f4xx_hash_sha1.c stm32f4xx_i2c.c stm32f4xx_iwdg.c stm32f4xx_pwr.c stm32f4xx_rcc.c stm32f4xx_rng.c \
stm32f4xx_rtc.c stm32f4xx_sdio.c stm32f4xx_spi.c stm32f4xx_syscfg.c stm32f4xx_tim.c stm32f4xx_usart.c \
stm32f4xx_wwdg.c
OBJ = $(SRC:.c=.o)
%.o : %.c
$(CC) -c -o $# $< $(CFLAGS)
all: $(OBJ)
$(CC) -o $# $^ $(CFLAGS)
I know this is terribly wrong, I'm not an expert with working in Makefile's, but I want to get this right, as it helps to understand the process a bit better.
What's basically required is to take the list of .c files in $(SRC) and build them into a list of .o files, which are output in lib/out
I know that my target and %.o... rule is horribly messed up. How do I get the all: target to build the individual .o files.
Here's the project structure just for reference, the Makefile I'm working on is in the ./lib folder.
.
├── Makefile
├── inc
│ └── main.h
├── lib
│ ├── Makefile
│ ├── inc
│ │ ├── cmsis
│ │ │ ├── arm_common_tables.h
│ │ │ ├── ...
│ │ ├── peripherals
│ │ │ ├── misc.h
│ │ │ ├── stm32f4xx_adc.h
│ │ │ ├── ...
│ │ └── stm32f4xx
│ │ ├── stm32f4xx.h
│ │ ├── stm32f4xx_conf.h
│ │ └── system_stm32f4xx.h
│ ├── src
│ │ └── peripherals
│ │ ├── misc.c
│ │ ├── stm32f4xx_adc.c
│ │ ├── ...
│ ├── startup_stm32f4xx.s
│ └── ~Makefile
├── readme.md
├── src
│ └── main.c
├── stm32f4.ld
├── stm32f4discovery.cfg
├── system_stm32f4xx.c
└── ~Makefile
typo OBJ = $(SRCS:.c=.o) change to OBJ = $(SRC:.c=.o), also looks like there is something messy with your dependency file generation.