├── .CMakeLists.txt.swp ├── .cproject ├── .gitignore ├── .gitmodules ├── .project ├── BindConfig.txt ├── CMakeLists.txt ├── README.md ├── demo ├── demo.go └── src │ └── bridge │ ├── bridge.go │ └── bridge.go.in ├── include └── junk.h ├── media └── win32.png └── src └── junk.c /.CMakeLists.txt.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowmint/go-static-linking/d364de875267ffc722f37437474917cff3f14bde/.CMakeLists.txt.swp -------------------------------------------------------------------------------- /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | build-* 3 | *.pyc 4 | *.exe 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/n"] 2 | path = deps/n 3 | url = doug@shadowmint.xen.prgmr.com:repo/libn.git 4 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | vdo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /BindConfig.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 Douglas Linder 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required (VERSION 2.8) 16 | SET(HERE "${CMAKE_ARGV3}") 17 | SET(INCLUDE_PATH "${CMAKE_ARGV4}") 18 | SET(LIBRARY_PATH "${CMAKE_ARGV5}") 19 | message("${LIBRARY_PATH} ${INCLUDE_PATH}") 20 | configure_file(${HERE}/demo/src/bridge/bridge.go.in ${HERE}/demo/src/bridge/bridge.go) 21 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 Douglas Linder 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required (VERSION 2.8) 16 | project (GB) 17 | set(LIB "gb") 18 | 19 | # Debug mode? 20 | set(CMAKE_BUILD_TYPE Debug) 21 | 22 | # Flags 23 | if(NOT WIN32) 24 | set(CMAKE_C_FLAGS "-Wall -pedantic -Wimplicit-function-declaration -Wreturn-type -Wstrict-prototypes -Wextra -fPIC") 25 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 26 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") 27 | endif() 28 | 29 | # Collect source files 30 | file(GLOB_RECURSE SOURCES src/*.c*) 31 | 32 | # libary headers 33 | include_directories(${PROJECT_SOURCE_DIR}/include) 34 | 35 | # Build library 36 | add_library(${LIB} ${SOURCES}) 37 | target_link_libraries(${LIB}) 38 | 39 | # Generate cgo wrapper 40 | add_custom_command(TARGET gb 41 | POST_BUILD 42 | COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/BindConfig.txt ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include $ 43 | ) 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go-C static linked demo 2 | 3 | Quick demo to show how to statically link a c library to a go application. 4 | 5 | ## Quick start 6 | 7 | mkdir build 8 | cd build 9 | cmake .. 10 | make 11 | 12 | cd .. 13 | cd demo 14 | export GOPATH=`pwd` 15 | go build 16 | ./demo 17 | 18 | ## Windows 19 | 20 | Building and running on windows is complicated. Long story short, do this: 21 | 22 | mkdir build 23 | cd build 24 | cmake .. -G "MSYS Makefiles" 25 | make 26 | 27 | cd .. 28 | cd demo 29 | export GOPATH=`pwd` 30 | go build 31 | ./demo 32 | 33 | ### Windows notes 34 | 35 | You must use MSYS for cgo. Visual studio is not supported. 36 | Install MSYS and run the MSYS shell for the commands. 37 | 38 | You must *also* use the *right* MSYS. The default MSYS is usually 39 | 32-bit; the default windows install of go is 64-bit. That won't work. 40 | Either install the 64-bit version of MSYS, or the 32-bit version of 41 | go. 42 | 43 | You have to use the MSYS shell. Using the standard shell, or powershell 44 | won't work; the right commands are not the PATH and `go build` won't 45 | work. 46 | 47 | Once again, no, visual studio is not supported. If you run: 48 | 49 | cmake .. 50 | 51 | The default is to generate a visual studio projet. Don't use this, 52 | the static .lib file generated is not compatible. 53 | 54 | Finally, yes, it does actually work. :) 55 | 56 | ![yay](https://raw.github.com/shadowmint/go-static-linking/master/media/win32.png) 57 | -------------------------------------------------------------------------------- /demo/demo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "bridge" 4 | 5 | func main() { 6 | bridge.Run() 7 | } 8 | -------------------------------------------------------------------------------- /demo/src/bridge/bridge.go: -------------------------------------------------------------------------------- 1 | package bridge 2 | 3 | import "fmt" 4 | 5 | // #cgo CFLAGS: -IC:/projects/go-static-linking/include 6 | // #cgo LDFLAGS: C:/projects/go-static-linking/build/libgb.a 7 | // #include 8 | import "C" 9 | 10 | func Run() { 11 | fmt.Printf("Invoking c library...\n") 12 | C.x(10) 13 | fmt.Printf("Done\n") 14 | } 15 | -------------------------------------------------------------------------------- /demo/src/bridge/bridge.go.in: -------------------------------------------------------------------------------- 1 | package bridge 2 | 3 | import "fmt" 4 | 5 | // #cgo CFLAGS: -I${INCLUDE_PATH} 6 | // #cgo LDFLAGS: ${LIBRARY_PATH} 7 | // #include 8 | import "C" 9 | 10 | func Run() { 11 | fmt.Printf("Invoking c library...\n") 12 | C.x(10) 13 | fmt.Printf("Done\n") 14 | } 15 | -------------------------------------------------------------------------------- /include/junk.h: -------------------------------------------------------------------------------- 1 | int x(int y); 2 | -------------------------------------------------------------------------------- /media/win32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowmint/go-static-linking/d364de875267ffc722f37437474917cff3f14bde/media/win32.png -------------------------------------------------------------------------------- /src/junk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int x(int y) { 5 | printf("Hello World\n"); 6 | return y; 7 | } 8 | --------------------------------------------------------------------------------