Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/link: ld: library not found for -ltest #20847

Closed
Shashwatsh opened this issue Jun 29, 2017 · 6 comments
Closed

cmd/link: ld: library not found for -ltest #20847

Shashwatsh opened this issue Jun 29, 2017 · 6 comments

Comments

@Shashwatsh
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8.3

What operating system and processor architecture are you using (go env)?

darwin/amd64 (MacOS)

What did you do?

I'm trying to build a daemon in Go and partly C

every time I try to compile ld throws an error

# _/Applications/Gogland_1.0_EAP.app/Contents/bin/Go/Users/shashwat/GoglandProjects/Squll-server/client/src/github.com/Shashwatsh/test/test4
ld: library not found for -ltest
clang: error: linker command failed with exit code 1 (use -v to see invocation)

main.go

package main

import "io"

/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -L . -ltest
#include "test.h"
 */
import "C"
import (
	//"io"
	// "io/ioutil"
	"log"
	"os"
	"fmt"
)

var (
	Trace   *log.Logger
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
)

func Init(
	traceHandle io.Writer,
	infoHandle io.Writer,
	warningHandle io.Writer,
	errorHandle io.Writer) {

	Trace = log.New(traceHandle,
		"TRACE: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Info = log.New(infoHandle,
		"INFO: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Warning = log.New(warningHandle,
		"WARNING: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Error = log.New(errorHandle,
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}

func main() {

	// Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
	f, err := os.OpenFile("testlogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
	if err != nil {
		fmt.Println("error opening file: %v", err)
	}
	defer f.Close()

	log.SetOutput(f)
	//aaa()
	//C.printhello()
	C.test()

	//Info.Println("Special Information")
	//Info.Println("Special Information")
}

//export t
func t(st int) {
	log.Print(st)
	//log.Println("Hello, World")
}

test.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "_cgo_export.h"

// extern void t();
int printhello(void)
{
  printf("Hello World\n");
  return 0;
}
int test(void)
{
	pid_t process_id = 0;
	pid_t sid = 0;
	// Create child process
	process_id = fork();
	// Indication of fork() failure
	if (process_id < 0)
	{
		printf("fork failed!\n");
		// Return failure in exit status
		exit(1);
	}
	// PARENT PROCESS. Need to kill it.
	if (process_id > 0)
	{
		printf("process_id of child process %d \n", process_id);
		// return success in exit status
		exit(0);
	}
	//unmask the file mode
	umask(0);
	//set new session
	sid = setsid();
	if(sid < 0)
	{
		// Return failure
		exit(1);
	}
	// Change the current working directory to root.
	chdir("/");
	// Close stdin. stdout and stderr
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);
	t(getpid());
	t(42);
	return 0;
}

test.h

 int test();
 int printhello();

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

well...a compiled application with no errors

What did you see instead?

# _/Applications/Gogland_1.0_EAP.app/Contents/bin/Go/Users/shashwat/GoglandProjects/Squll-server/client/src/github.com/Shashwatsh/test/test4
ld: library not found for -ltest
clang: error: linker command failed with exit code 1 (use -v to see invocation)
@Shashwatsh
Copy link
Author

Shashwatsh commented Jun 29, 2017

@ALTree sorry about that go build and I also tried with go build -ldflags="-linkmode internal"

@ALTree
Copy link
Member

ALTree commented Jun 29, 2017

@Shashwatsh but are you building the c library with gcc before running go build? Does it work?

@Shashwatsh
Copy link
Author

wait! i have to first build the C library sorry i thought cgo would do that for me

@Shashwatsh
Copy link
Author

let me try that

@Shashwatsh
Copy link
Author

test.c:7:10: fatal error: '_cgo_export.h' file not found
#include "_cgo_export.h"
         ^
1 error generated.

How do i generate cgo header files?

@ALTree
Copy link
Member

ALTree commented Jun 29, 2017

wait! i have to first build the C library sorry i thought cgo would do that for me

That's likely the problem. Let's take this discussion somewhere else (the go project does not use the issue tracker for general discussion and questions about the language, it's just for issues). See: https://github.com/golang/go/wiki/Questions. For example golang-nuts. Thanks.

I don't think there's a bug here: you just need help with understanding cgo.

How do i generate cgo header files?

Remove that #include "_cgo_export.h", you don't need it.

@ALTree ALTree closed this as completed Jun 29, 2017
@mikioh mikioh changed the title ld: library not found for -ltest cmd/link: ld: library not found for -ltest Aug 2, 2017
@golang golang locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants