'error: linking with `cc` failed: exit code: 1' Error when calling methods on nested structs


#1

Not sure of this is the best place for support but I’m constantly getting an error trying to access a method on struct.

Essentially I have a struct which has a member (another struct) and I’m trying to call a method on that member, unfortunately that seems to give me the following unhelpful error:

I’m still in the process of learning rust so any help would be greatly appreciated, I’ve popped the code on github: https://github.com/davidrhyswhite/rust-github

note: "cc" '"-m64"' '"-L"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib"' '"-o"' '"/Users/david/Projects/open-source/rust-github/target/examples/get_user"' '"/Users/david/Projects/open-source/rust-github/target/examples/get_user.o"' '"-Wl,-force_load,/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a"' '"-Wl,-dead_strip"' '"-nodefaultlibs"' '"/Users/david/Projects/open-source/rust-github/target/libgithub-9ab9bc8ca765efad.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/libcurl-0f9d123c81e5c8e9.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/libcurl-sys-5c9efd7870af2b22.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/liburl-9219287afc41ddab.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/libmatches-68db25b520030534.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/liblibz-sys-210936a89f013a45.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/librustc-serialize-3bbc0a7cc83f04eb.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/libopenssl-sys-2929c0814229fdf1.rlib"' '"/Users/david/Projects/open-source/rust-github/target/deps/liblibc-8d21de95f4de7169.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcollections-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libunicode-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librand-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e7c5e5c.rlib"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-4e7c5e5c.rlib"' '"-L"' '"/Users/david/Projects/open-source/rust-github/target"' '"-L"' '"/Users/david/Projects/open-source/rust-github/target/deps"' '"-L"' '"/usr/lib"' '"-L"' '"/usr/lib"' '"-L"' '"/Users/david/Projects/open-source/rust-github/target/build/openssl-sys-2929c0814229fdf1/out"' '"-L"' '"/usr/local/lib/rustlib/x86_64-apple-darwin/lib"' '"-L"' '"/Users/david/Projects/open-source/rust-github/.rust/lib/x86_64-apple-darwin"' '"-L"' '"/Users/david/Projects/open-source/rust-github/lib/x86_64-apple-darwin"' '"-lcurl"' '"-lz"' '"-lssl"' '"-lcrypto"' '"-lz"' '"-lc"' '"-lm"' '"-lSystem"' '"-lpthread"' '"-lc"' '"-lm"' '"-lcompiler-rt"'
note: ld: warning: directory not found for option '-L/Users/david/Projects/open-source/rust-github/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/david/Projects/open-source/rust-github/lib/x86_64-apple-darwin'
Undefined symbols for architecture x86_64:
  "user::UserService::get::hf5f029389c593b65Wba", referenced from:
      main::hcf79066d900a9716gaa in get_user.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

#2

@davidrhyswhite I’m new too, but I forked your repo and made some changes in my develop branch, I got a similar error to you when I tried to run cargo run. After I commented out extern crate curl; on Line 4 of lib.rs the error changed to just the following:

src/client.rs:4:5: 4:9 error: unresolved import `curl::http`. Did you mean `self::curl`?
src/client.rs:4 use curl::http;

Then when I tried following the compiler’s advice by changing to use self::curl::http; in client.rs, the original long error message came back again.


#3

Hi @ltfschoen,

I’ve narrowed the problem down to calling a method on a struct inside another struct. Essentially have the following:

// src/user.rs
pub struct UserService {
    c: Client,
}

impl UserService {

    pub fn new(c: Client) -> UserService {
        UserService;
    }

    pub fn get(&self) {
        println!("#get method called");
    }
}

// src/lib.rs
pub struct Github {
    pub users: UserService,
}

impl Github {
    pub fn new() -> Github {
        Github { 
            users: UserService::new(),
        }
    }
}

// examples/get_user.rs
fn main() {
    let g = Github::new();
    g.users.get();
}

If I comment out the g.users.get(); it seems to run fine. You can run the example on it’s own with cargo run --example get_user


#4

Setup a new, very simple example of this with the following cargo project, https://github.com/davidrhyswhite/rust-structs-example

Inside: src/lib.rs

#![crate_name = "structs-example"]
#![crate_type = "rlib"]

use child::Child;

pub struct Parent {
    pub child: Child
}

impl Parent {
    pub fn new() -> Parent {
        Parent { child: Child::new() }
    }
}

mod child;

Inside: src/child.rs

pub struct Child;

impl Child {

    pub fn new() -> Child {
        Child
    }

    pub fn method(self) {
        println!("Method called");
    }
}

Inside examples/example.rs

rate “structs-example” as structs;

use structs::Parent;

fn main() {
    let parent = Parent::new();
    parent.child.method();
}

Running the example with cargo run --example example causes the cc linking error.

Not sure if anyone has had trouble with this before?

Thanks

David