Issues linking a C library module using swift build


(Valentin Radu) #1

Hello all,

I’m having some strange issues on osx when trying to link a C library module. I’ve successfully did this before with another library, however, for some reason this one doesn’t work.
My module map looks like this:

module CFCGI [system] {
    header "/usr/local/include/cfcgi/cfcgi.h"
    link "cfcgi"
    export *
}

And the output:

swift build
Compiling Swift Module 'April' (1 sources)
Linking Executable: .build/debug/April
Undefined symbols for architecture x86_64:
  "_cfcgi_create", referenced from:
      _main in main.swift.o
  "_cfcgi_free", referenced from:
      _main in main.swift.o
  "_cfcgi_start_and_wait", referenced from:
      _main in main.swift.o
ld: symbol(s) not found for architecture x86_64

It looks like the linker ignores the `link "cfcgi”` part.
I tried lots of things but failed to find the reason behind this. I’m starting to think this might be related to this paragraph in the clang docs:

"Automatic linking with the link directive is not yet widely implemented, because it requires support from both the object file format and the linker. The notion is similar to Microsoft Visual Studio’s #pragma comment(lib…).”

Unfortunately I find it a little vague and I can’t know for sure if it applies here.

So far, this is what I know for sure:
- libcfcgi.a is in /usr/local/lib and it’s compiled for x86_64
- libcfcgi.a contains the above mentioned symbols, tested it with nm and linked it in a small C program
- I’m on osx and build with the swift-2.2-SNAPSHOT-2015-12-01-a toolchain

Any input on this would be greatly appreciated!

Valentin Radu


(Max Howell) #2

Can you post the output from: `swift build --clean && swift build -v`, thanks.

···

On Dec 14, 2015, at 4:30 PM, Valentin Radu via swift-dev <swift-dev@swift.org> wrote:

Hello all,

I’m having some strange issues on osx when trying to link a C library module. I’ve successfully did this before with another library, however, for some reason this one doesn’t work.
My module map looks like this:

module CFCGI [system] {
    header "/usr/local/include/cfcgi/cfcgi.h"
    link "cfcgi"
    export *
}

And the output:

swift build
Compiling Swift Module 'April' (1 sources)
Linking Executable: .build/debug/April
Undefined symbols for architecture x86_64:
  "_cfcgi_create", referenced from:
      _main in main.swift.o
  "_cfcgi_free", referenced from:
      _main in main.swift.o
  "_cfcgi_start_and_wait", referenced from:
      _main in main.swift.o
ld: symbol(s) not found for architecture x86_64

It looks like the linker ignores the `link "cfcgi”` part.
I tried lots of things but failed to find the reason behind this. I’m starting to think this might be related to this paragraph in the clang docs:

"Automatic linking with the link directive is not yet widely implemented, because it requires support from both the object file format and the linker. The notion is similar to Microsoft Visual Studio’s #pragma comment(lib…).”

Unfortunately I find it a little vague and I can’t know for sure if it applies here.

So far, this is what I know for sure:
- libcfcgi.a is in /usr/local/lib and it’s compiled for x86_64
- libcfcgi.a contains the above mentioned symbols, tested it with nm and linked it in a small C program
- I’m on osx and build with the swift-2.2-SNAPSHOT-2015-12-01-a toolchain

Any input on this would be greatly appreciated!

Valentin Radu

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(Valentin Radu) #3

Auch, sorry, wrong list. Will repost in users.

···

On Tue 15 Dec, 15, at 02:30, Valentin Radu via swift-dev <swift-dev@swift.org> wrote:

Hello all,

I’m having some strange issues on osx when trying to link a C library module. I’ve successfully did this before with another library, however, for some reason this one doesn’t work.
My module map looks like this:

module CFCGI [system] {
    header "/usr/local/include/cfcgi/cfcgi.h"
    link "cfcgi"
    export *
}

And the output:

swift build
Compiling Swift Module 'April' (1 sources)
Linking Executable: .build/debug/April
Undefined symbols for architecture x86_64:
  "_cfcgi_create", referenced from:
      _main in main.swift.o
  "_cfcgi_free", referenced from:
      _main in main.swift.o
  "_cfcgi_start_and_wait", referenced from:
      _main in main.swift.o
ld: symbol(s) not found for architecture x86_64

It looks like the linker ignores the `link "cfcgi”` part.
I tried lots of things but failed to find the reason behind this. I’m starting to think this might be related to this paragraph in the clang docs:

"Automatic linking with the link directive is not yet widely implemented, because it requires support from both the object file format and the linker. The notion is similar to Microsoft Visual Studio’s #pragma comment(lib…).”

Unfortunately I find it a little vague and I can’t know for sure if it applies here.

So far, this is what I know for sure:
- libcfcgi.a is in /usr/local/lib and it’s compiled for x86_64
- libcfcgi.a contains the above mentioned symbols, tested it with nm and linked it in a small C program
- I’m on osx and build with the swift-2.2-SNAPSHOT-2015-12-01-a toolchain

Any input on this would be greatly appreciated!

Valentin Radu


(Valentin Radu) #4

Sure:

Cloning into '/Users/valentinradu/Playgrounds/April/Packages/libcfcgi'...
warning: --depth is ignored in local clones; use file:// instead.
done.
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -module-name April -incremental -emit-dependencies -emit-module -emit-module-path /Users/valentinradu/Playgrounds/April/.build/debug/April.swiftmodule -output-file-map /Users/valentinradu/Playgrounds/April/.build/debug/April.o/April/output-file-map.json -c /Users/valentinradu/Playgrounds/April/Sources/main.swift -I /Users/valentinradu/Playgrounds/April/.build/debug -j8 -Onone -g -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Xcc -F-module-map=/Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0/module.modulemap -I /Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0 -I /usr/local/include
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -o /Users/valentinradu/Playgrounds/April/.build/debug/April /Users/valentinradu/Playgrounds/April/.build/debug/April.o/Sources/main.swift.o -Xlinker -all_load -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/usr/local/lib
Undefined symbols for architecture x86_64:
  "_cfcgi_create", referenced from:
      _main in main.swift.o
  "_cfcgi_free", referenced from:
      _main in main.swift.o
  "_cfcgi_start_and_wait", referenced from:
      _main in main.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift-build-tool", "-v", "-f", "/Users/valentinradu/Playgrounds/April/.build/debug/April.o/llbuild.yaml"]


(Max Howell) #5

Moving to swift-build-dev

Can we get your module.map too?

It is also worth checking that your module map package is tagged and all sources git-added.

···

On Dec 14, 2015, at 4:40 PM, Valentin Radu <radu.v.valentin@me.com> wrote:

Sure:

Cloning into '/Users/valentinradu/Playgrounds/April/Packages/libcfcgi'...
warning: --depth is ignored in local clones; use file:// instead.
done.
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -module-name April -incremental -emit-dependencies -emit-module -emit-module-path /Users/valentinradu/Playgrounds/April/.build/debug/April.swiftmodule -output-file-map /Users/valentinradu/Playgrounds/April/.build/debug/April.o/April/output-file-map.json -c /Users/valentinradu/Playgrounds/April/Sources/main.swift -I /Users/valentinradu/Playgrounds/April/.build/debug -j8 -Onone -g -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Xcc -F-module-map=/Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0/module.modulemap -I /Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0 -I /usr/local/include
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -o /Users/valentinradu/Playgrounds/April/.build/debug/April /Users/valentinradu/Playgrounds/April/.build/debug/April.o/Sources/main.swift.o -Xlinker -all_load -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/usr/local/lib
Undefined symbols for architecture x86_64:
"_cfcgi_create", referenced from:
     _main in main.swift.o
"_cfcgi_free", referenced from:
     _main in main.swift.o
"_cfcgi_start_and_wait", referenced from:
     _main in main.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift-build-tool", "-v", "-f", "/Users/valentinradu/Playgrounds/April/.build/debug/April.o/llbuild.yaml"]


(Valentin Radu) #6

The module map only contains this module:

module CFCGI [system] {
    header "/usr/local/include/cfcgi/cfcgi.h"
    link "cfcgi"
    export *
}

The packaged is tagged and git added.

···

On Tue 15 Dec, 15, at 02:44, Max Howell <max.howell@apple.com> wrote:

Moving to swift-build-dev

Can we get your module.map too?

It is also worth checking that your module map package is tagged and all sources git-added.

On Dec 14, 2015, at 4:40 PM, Valentin Radu <radu.v.valentin@me.com> wrote:

Sure:

Cloning into '/Users/valentinradu/Playgrounds/April/Packages/libcfcgi'...
warning: --depth is ignored in local clones; use file:// instead.
done.
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -module-name April -incremental -emit-dependencies -emit-module -emit-module-path /Users/valentinradu/Playgrounds/April/.build/debug/April.swiftmodule -output-file-map /Users/valentinradu/Playgrounds/April/.build/debug/April.o/April/output-file-map.json -c /Users/valentinradu/Playgrounds/April/Sources/main.swift -I /Users/valentinradu/Playgrounds/April/.build/debug -j8 -Onone -g -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Xcc -F-module-map=/Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0/module.modulemap -I /Users/valentinradu/Playgrounds/April/Packages/libcfcgi-1.0.0 -I /usr/local/include
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -o /Users/valentinradu/Playgrounds/April/.build/debug/April /Users/valentinradu/Playgrounds/April/.build/debug/April.o/Sources/main.swift.o -Xlinker -all_load -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/usr/local/lib
Undefined symbols for architecture x86_64:
"_cfcgi_create", referenced from:
    _main in main.swift.o
"_cfcgi_free", referenced from:
    _main in main.swift.o
"_cfcgi_start_and_wait", referenced from:
    _main in main.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift-build-tool", "-v", "-f", "/Users/valentinradu/Playgrounds/April/.build/debug/April.o/llbuild.yaml"]